Al-Mahdi was born in 744 or 745 AD in the village of Hamimah. His mother was called Arwi, and his father was al-Mansur. When al-Mahdi was ten years old, his father became the second Abbasid Caliph. When al-Mahdi was young, his father, the Caliph al-Mansur, needed to establish al-Mahdi as a powerful figure in his own right. So, on the east bank of the Tigris, al-Mansur oversaw the construction of East Baghdad, with a mosque and royal palace at its heart. Construction in the area was also heavily financed by the Barmakids, and the area became known as Rusafa.
When he was 15-years-old, a-Mahdi was sent to defeat the uprising of Abdur Rahman bin Abdul Jabbar Azdi in Greater Khorasan. He also defeated the uprisings of Ispahbud, the governor of Tabaristan, and Astazsis , massacring more than 70,000 of his followers in Khorasan. These campaigns put Tabaristan, which was only nominally within the caliphate, firmly under Abbasid control. In 762 AD, al-Mahdi was the governor of the Abbasid Caliphate's eastern region, based in Ray. It was here that he fell in love with al-Khayzuran and had several children, including the fourth and fifth future Caliphs, al-Hadi and Harun al-Rashid. Around 770 AD (153 AH), al-Mahdi was appointed as Amir al-hajj.
Al-Mahdi's father, Al-Mansur, died on the hajj to Mecca in 775. The throne then passed to Al-Mansur's chosen successor, his son Al-Mahdi. According to Marozzi, "[it] was, by the standards of the future, blood-soaked successions of the Abbasid caliphate, a model of order and decorum."
Al-Mahdi, whose nickname means "Rightly-guided" or "Redeemer", was proclaimed caliph when his father was on his deathbed. His peaceful reign continued the policies of his predecessors.
Rapprochement with the Alids in the Caliphate occurred under al-Mahdi's reign. The powerful Barmakid family, which had advised the Caliphs since the days of Abu al-‘Abbās as viziers, gained even greater powers under al-Mahdi's rule, and worked closely with the caliph to ensure the prosperity of the Abbasid state.
Al-Mahdi reigned for ten years. He imprisoned his most trusted vizier Ya'qub ibn Dawud. In the year 167 AH/ 783 AD, al-Mahdi instituted an official inquisition which led to the execution of alleged Zindiq (heretics). He was fond of music and poetry and during his caliphate many musicians and poets received his patronage and he supported musical expression and poetry across his dominion; accordingly, his son Ibrahim ibn al-Mahdi (779–839) and his daughter ‘Ulayya bint al-Mahdī (777-825) were both noted poets and musicians.
In 775, a Byzantine envoy, Tarath, travelled to Baghdad to convey the congratulations of the Byzantine emperor to Al-Mahdi on his accession to the throne. Tarath was so pleased with the hospitality he received that he offered to put his engineering knowledge to use and build a mill that would generate annual profits, of 500,000 dirhams, equal to the cost of its construction. On completion, the envoy's forecast proved to be correct, and so, delighted, Al-Mahdi ordered that all profits should be given to the envoy, even after he left Baghdad. It is believed this continued to his death, in 780.
In 777 AD (160 AH) he put down the insurrection of Yusuf ibn Ibrahim in Khurasan. In the same year al-Mahdi deposed Isa ibn Musa as his successor and appointed his own son Musa al-Hadi in his place and took allegiance (bayah) for him from the nobles. In 778 AD (161 AH), he subdued the rebellion of Abdullah ibn Marwan ibn Muhammad, who was leading the Umayyad remnant in Syria.
Al-Mahdi was poisoned by one of his concubines in 785 AD (169 AH).
The cosmopolitan city of Baghdad blossomed during al-Mahdi's reign. The city attracted immigrants from Arabia, Iraq, Syria, Persia, and lands as far away as Afghanistan and Spain. Baghdad was home to Christians, Jews, Hindus and Zoroastrians, in addition to the growing Muslim population. It became the world's largest city.
Al-Mahdi continued to expand the Abbasid administration, creating new diwans, or departments: for the army, the chancery, and taxation. Qadis or judges were appointed, and laws against non-Arabs were dropped.
The Barmakid family staffed these new departments. The Barmakids, who were of Persian extraction, had originally been Buddhists. Their short-lived Islamic legacy would count against them during the reign of Harun al-Rashid.
The introduction of paper from China (see Battle of Talas) in 751 had a profound effect. Paper had not yet been used in the West with the Arabs and Persians using papyrus and the Europeans using vellum. The paper related industry boomed in Baghdad where an entire street in the city center became devoted to sale of paper and books. The cheapness and durability of paper was vital element in the efficient growth of the expanding Abbasid bureaucracy.
Al-Mahdi had two important religious policies: the persecution of the zanadiqa, or dualists, and the declaration of orthodoxy. Al-Mahdi focused on the persecution of the zanadiqa in order to improve his standing among the purist Shi'i, who wanted a harder line on heresies, and found the spread of syncretic Muslim-polytheist sects to be particularly virulent. Al-Mahdi declared that the caliph had the ability, and indeed the responsibility, to define the orthodox theology of Muslims to protect the umma against heresy. Al-Mahdi made great use of this broad, new power, and it would become important during the 'mihna' crisis of al-Ma'mun's reign.