Idris was one of the family members of Ali’s descendants who attempted to rebel against the Abbasid Caliphs. His great-grandfather was Hassan, the older son of Ali and Fatimah. His half-brother was al-Nafs al-Zakiyyah, the Pure-Hearted One, whose uprising against Caliph Mansur had lasted nearly a year before its inevitable crushing. Idris participated in a 786 uprising near Mecca. Survivors blended in with Hajj pilgrims and slipped away.
Idris went to Egypt, then into North Africa. The safest place for him was to be as far from Baghdad as possible, so he continued until he reached Morocco. At the ruined Roman fort of Volubilis, now Walila, he joined the resident Berber tribe by marriage. They proclaimed him their Imam. With a Berber army, Idris conquered much of northern Morocco and some of Algeria, including the city of Tlemcen. Baghdad didn’t mind that much if Morocco went rogue, but moving eastward across North Africa meant a real threat. On orders of Haroun al-Rashid, assassins poisoned Idris in 791.
But Idris’ Berber wife delivered a baby boy two months after his death. This child, Idris II, was raised among his mother’s Berber people, but they were quite aware that the Prophet’s bloodline was carried on in him. He was proclaimed Imam after his father, and the Berbers were careful to have him educated in Islamic traditions. Although they were of the family of Ali, the education Idris II received was more Sunni than Shi’ite, probably just by geography and circumstance, not intent.
Idris I and II, father and son, founded the city of Fez to be their capital. The Idrisid dynasty seems to have been competent and Morocco flourished. Their independent kingdom lasted until they were expelled from Fez in 927 by the rising Fatimid dynasty. Islam was firmly rooted under their rule; the local Berbers all entered Islam. Morocco also became very Arabized, with its court in Fez welcoming other refugees from the Arab heartland.
The current ruling dynasty claims descent from Hassan, like Idris, but they came to Morocco three centuries later.