Islam has been part of Morocco since around 670 AD when the Umayyads, under their general Uqba ibn Nafi, conquered most of the Maghreb, which includes modern Morocco, Tunisia, Algeria and Lybia. Following that conquest the indigenous Berber population slowly converted to Islam.
In 788 AD Idris I (Moulay Idris) is credited as founding the first Islamic dynasty in Morocco, the Idrisid dynasty (although it was not until the 11th century that the Almoravids created an empire that included almost all of modern Morocco and making the Maliki school of Islam predominate). From that time most of the region covering modern Morocco was ruled by Islamic dynasties (either Berber or Arab), despite Portuguese Christian incursions in the 16th century, other than the French and Spanish Christian protectorates of the early 20th century.
In the second half of the 7th century, the soldiers of the Prophet Mohammed set forth from the Arabian Peninsula and overwhelmed the peoples of North Africa. Within a century, nearly all Berber tribes had embraced Islam, although, true to form, local tribes developed their own brand of Islamic Shi’ism, which sparked rebellion against the eastern Arabs.
By 829, local elites had established an Idrissid state with its capital at FES, dominating all of Morocco. Thus commenced a cycle of rising and falling Islamic dynasties, which included the Almoravids (1062–1147), who built their capital at MARRAKECH ; the Almohads (1147–1269), famous for building the Koutoubia Mosque; the Merenids (1269–1465), known for their exquisite mosques and madrassas (Quranic schools), especially in FES the Saadians (1524–1659), responsible for the Palais el-Badi in MARRAKECH; and the Alawites (1659–present).