Medieval sources report that the Hejaz remained under Ikhshidid suzerainty until the
Fatimid conquest of Egypt in 969, with the
Friday sermon being read in the name of the Ikhshidid emir. But power in the city was seized, already by , by a
Husaynid Alid, Ubayd Allah ibn Tahir, in circumstances that are entirely unknown. Ubayd Allah's successors, the Banu Muhanna dynasty, would rule Medina on and off until the sixteenth century. This development was paralleled a generation later at Mecca, where a
Hasanid, Ja'far ibn Muhammad, seized control. The
Sharifate of Mecca was much larger and powerful than that of Medina, which apparently controlled little beyond the city's immediate environs. The two emirates would be often in conflict with one another, as the more ambitious sharifs of Mecca tried to include Medina in their domain. The two emirates were also exposed to the rivalries of the great powers of the Muslim world, who by turns wooed and pressured the sharifs to recognize their suzerainty, and exploited dynastic rivalries or used direct military force to impose their preferred candidates as emirs; the sharifs of Medina were adherents of
Twelver Shi'ism (as those of Mecca were
Zaydi Shi'a), and usually recognized the suzerainty of the
Isma'ili Shi'a Fatimid caliphs and mentioned them in the Friday sermon. Ubayd Allah's son
Muslim had settled in Egypt, and was a friend of the Ikhshidid emir,
Abu al-Misk Kafur, but appears to have switched his allegiance to the
Fatimid caliph al-Mu'izz soon after Kafur's death, even before the Fatimid conquest of Egypt, and may have played a role in the success of the latter. The Friday sermon was read for al-Mu'izz in Medina in 969 or 970, and two years later the Sharif of Medina joined the Fatimids in a campaign that obliged the Sharif of Mecca to acknowledge Fatimid suzerainty as well. After Muslim died in 976/7, his son
Tahir returned from Egypt to Medina, where he was acknowledged as emir of the city by the local Alids. Initially acknowledging the suzerainty of the Abbasids, the arrival of a Fatimid army forced him to return to Fatimid allegiance. Later the Banu Muhanna briefly lost control of Medina to the Meccan emir
Abu'l-Futuh al-Hasan ibn Ja'far in at the behest of the Fatimids, and again to Abu'l-Futuh's son,
Shukr (), and to the first
Hawashim emir of Mecca,
Abu Hashim Muhammad ibn Ja'far (). Otherwise little is known about the history of Medina in the 11th and 12th centuries, and even the exact line of succession between the various branches of the Banu Muhanna is unclear. More information is available on
Qasim ibn Muhanna, who ruled from till the 1190s, as he was a friend and confidant of the
Ayyubid sultan of Egypt,
Saladin. In 1176, Qasim joined the
Hajj caravan to Mecca; the ,
Tashtakin al-Mustanjadi, deposed the sharif of Mecca,
Mukaththir ibn Isa, and installed Qasim as the new ruler of Mecca. Realizing that his position was untenable, after only three days Qasim handed over power to Mukaththir's brother,
Da'ud. Qasim' son and successor,
Salim, in turn had to confront the attacks of
Qatada ibn Idris, who deposed Mukaththir in 1203 and assumed control of Mecca. Salim was able to secure the assistance of the Ayyubids, and his nephew and successor, Qasim, was able to defeat the Meccans in battle at Wadi al-Safra in 1216. Qasim's attacks on Mecca proved fruitless, and the Ayyubids of Egypt, interested in maintaining a balance of power, switched their support to the Meccans and even garrisoned
Yanbu and possible Mecca itself for protection. The long reign of
Shihah ibn Hashim that followed (1226/7–1249/50) was peaceful and prosperous, and marked by the close relations with Ayyubid Egypt, which led Shihah to support and even lead repeated Ayyubid attempts to regain control of Mecca, which was being contested by the
Rasulids of
Yemen. Shihah's son,
Jammaz, continued the attempts to annex Mecca, but was only briefly successful, occupying the city for 40 days in 1271 and a few months in 1288. His reign also marks the start of the tightening of control by the
Mamluk Sultanate of Cairo over Medina: in 1283/4 the Mamluks installed a
Sunni imam in the
Prophet's Mosque, and the Mamluk sultans began to interfere more actively in the affairs of Medina. In this they were aided by dynastic disputes among the Banu Muhanna; by the 14th century, Cairo had arrogated the right to appoint the emir, while the latter was downgraded to the status of a mere Mamluk functionary. The disputes began with Jammaz's twelve sons: the chosen successor,
Mansur, was opposed by his brothers Muqbil and Wudayy; both Muqbil and his son, Barjis, turned to Cairo for sanction of their claims to the emirate, while Mansur in turn visited Cairo to secure his own position. Both Mansur and his son and successor,
Badr al-Din Kubaysh, lost control of Medina to rival brothers, nephews, or cousin during their absence, and were able to recover it only through Mamluk intercession. Both Mansur and Kubaysh were assassinated by rival family members. Mansur's descendants remained in power after, with the exception of
Wudayy ibn Jammaz's rule in 1336–1343, and a period when the family was ousted from Medina in 1350–1357/8. Finally, in the 15th century, Medina was subordinated to the Sharif of Mecca, who became the 'vice-sultan' of the Hejaz, starting with
Hasan ibn Ajlan; but this also meant that both Mecca and Medina, as well as the Hejaz more broadly, gradually became more tightly integrated into the Mamluk empire. By 1426, the Mamluk sultan even demanded the payment of large sums of money from the emirs of Mecca and Medina as a tribute before they were confirmed in office. Emir
Ajlan ibn Nu'ayr was deposed when
Khashram ibn Dawghan promised to pay Sultan
Barsbay 5,000
gold dinars, but Khashram was in turn deposed a year later after failing to forward the sum. By 1439/40, and likely since 1424, when it is attested for Mecca, a small Mamluk garrison was sent yearly to Medina to maintain order and further cement Cairo's control. ==Emirs==