The Mahdids (بني مهدي, Banī Mahdī) were a dynasty in Yemen who briefly held power in the period between 1159 and 1174. In spite of the name they did not represent a Shia Muslim Mahdi movement, but rather followed the Hanafi madhhab (law school) of Sunni Islam.
Their name is derived from their first ruler Ali bin Mahdi who was born in Tihama. Ali bin Mahdi in his turn, traced his ancestry back to the ancient kings of Himyar. He was a religious and wide-travelled figure who performed the hajj every year and met scholars from all over the Muslim world. In 1136–1142 Ali bin Mahdi propagated his religious ideas in the Tihama lowland which at that time was ruled by the Najahids of Zabid. The Najahid queen 'Alam was initially attracted by his teachings and even exempted him and his followers from paying the kharaj. Building up a power base, he gathered an army in 1143 and attacked his benefactors. He made efforts to conquer the town al-Kadrā north of Zabid. This, however, failed. Ali and his followers withdrew to the mountains but were allowed back to the Najahid realm in 1146 at the insistence of queen 'Alam. After the death of the queen in 1150 a devastating war flared up between the Mahdids and Najahids. Ali subsequently tried to reach his aims by means of intrigues, undermining the Najahid regime which at this time was dominated by wazirs. His ambitions led to the murder of the leading Najahid figure Surūr al-Fātikī in 1156. The people of Zabid called in assistance from the Zaidiyyah imam al-Mutawakkil Ahmad in 1158 to meet the acute threat from the Mahdids, and promised to acknowledge him as their lord. The last Najahid ruler, al-Fatiq III, was murdered soon after by the imam or by his own guards. The imam, however, was unable to remain in Zabid for long and withdrew. Ali bin Mahdi established himself in the city but died soon after, in 1159.
Ali bin Mahdi was succeeded by his son Mahdi bin Ali, possibly co-ruling with his younger brother Abd an-Nabi. The sons of Ali consolidated the power relations in the Tihama. An advantageous peace was concluded with the Zurayids of Aden. At the same time the Mahdids attacked other areas in the south such as Lahij and Abyan in order to gain plunder. Mahdi bin Ali died in 1163 and his brother Abd an-Nabi gained full control. He was reputed as an exceedingly strict lord who imposed death penalty for anyone opposing his teachings, and for wine drinking, singing and illicit sexual intercourse (although other sources suggest that he was himself a drunkard and womanizer). He upheld egalitarian principles of common property within the community. Muslim historians usually denounce him as a half-crazed robber with world-ruling ambitions.