Morocco's first dynasty (788–974): Idris I and Idris II, founder of Fez.
The Idrisid dynasty (788–974) founded the first Moroccan state. Idris I, a descendant of the Prophet Muhammad fleeing Abbasid persecution, established an independent kingdom at Volubilis in 788; his son Idris II then founded Fez, the city that became Morocco's spiritual heart. The Idrisids invented the sharifian model of kingship that every later dynasty copied.
Idris ibn Abdallah fled Arabia after the failed revolt at Fakhkh in 786, found refuge among Berber tribes in the far west, and was proclaimed imam at Volubilis in 788. Poisoned in 791 — Abbasid agents are the usual suspects — he left a pregnant wife; the posthumous Idris II was raised by his Berber regent Rashid and, around 809, founded Fez. Islam and Arabic took root in Morocco under Idrisid rule, in a land of Berber confederacies.
The first capital was Walili — Volubilis — among the Roman ruins, a symbolic break with both empires: neither Rome's nor Baghdad's. Idris II's Fez, founded on both banks of the river, drew Arab immigrants from Kairouan and Andalusia and grew into the religious and intellectual capital of the far west, a rank it has never lost.
Idris I was a Hasanid — a descendant of the Prophet through Hasan, Muhammad's grandson. That lineage turned a refugee into a legitimate sovereign in the eyes of the faithful, and it became Morocco's political formula: the Saadian and Alaouite dynasties claim the same Prophetic blood. The Idrisids made descent from Muhammad the currency of Moroccan power.
After Idris II died in 828, his sons divided the realm; by the tenth century the Idrisids were squeezed between the Fatimid caliphate in the east and the Umayyads of Córdoba in the north, reduced to a rump state around Hajar an-Nasr. The last Idrisid was deposed in 974. But the idea survived: Fez kept growing, and the Idrisid mausoleums there remain among Morocco's holiest shrines.