The Mahdids (بني مهدي, Banī Mahdī) were a Himyarite 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.
Abd an-Nabi carried on the expansive policy of the dynasty, attacking the Sulaymanids in northern Tihama whose territories were annexed. The cities Ta'izz and Ibb fell in 1164 and Aden was besieged in the same year. The Zurayids of Aden allied with the Hamdanid sultan of San'a in 1172. Together they managed to defeat the Mahdid forces in 1173. Abd an-Nabi withdrew to Zabid. Shortly after these events, the Ayyubid prince Turan Shah led an expedition to southern Arabia. One of the motives of the Ayyubids to invade Yemen was reportedly the stance of the Mahdids who were considered heretics and associated by outsiders with the Kharijites. Moreover, they had stopped mentioning the Abbasid caliph in the Friday prayer, making them a legitimate object of conquest. Turan Shah found a willing ally in the Sulaymanids, whose ruler had been slain by the Mahdids nine years previously. The Ayyubid troops quickly overran the bulk of Yemen and took Zabid on 13 May 1174. The city was looted and Abd an-Nabi and his brother Ahmad were taken prisoners. Both of them were strangulated in 1176, probably due to an attempt to revive Mahdid rule, and with them the brief era of the Mahdids came to an end.