The Idrisid dynasty or Idrisids (الأدارسة ) were an Arab Muslim dynasty from 788 to 974, ruling most of present-day Morocco and parts of present-day western Algeria. Named after the founder, Idris I, the Idrisids were an Alid dynasty descended from Muhammad through his grandson Hasan. The Idrisids are traditionally considered to be the founders of the first Moroccan state, setting the stage for subsequent dynasties and states centered in this region. Their reign played an important role in the early Islamization of Morocco and also presided over an increase in Arab immigration and Arabization in major urban centers.
Fleeing the Abbasid Caliphate to the east in the aftermath of the Battle of Fakhkh, Idris I first established himself in 788 at Volubilis in present-day Morocco with the help of local Berber allies. He and his son, Idris II, subsequently founded what became the city of Fez further east. Fez became the capital of an Idrisid state which ruled most of present-day Morocco and part of western Algeria. After Idris II's death, the realm was divided between his rival sons. After a period of conflict, the dynasty's authority resumed and remained relatively stable between 836 and 863. In the late 9th century, however, they faced repeated challenges and local opposition. In the 10th century the region came under the political domination of Zenata tribes who fought proxy battles on behalf of two rival powers in the region, the Fatimid Caliphate and the Umayyad Caliphate of Córdoba. The Idrisids were definitively expelled from Fez in 927, but held onto to a reduced territory in the north of Morocco from their base at Hajar an-Nasr. They were finally defeated and removed from power in 974, and a brief attempt to regain power in 985 also failed.
By the second half of the 8th century the westernmost regions of the Maghreb, including present-day Morocco, had been effectively independent of the Umayyad Caliphate since the Khariji-led Berber revolts that started in 739–40. The Abbasid Caliphate after 750 had no more success in re-establishing control over Morocco.