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.