Under the Roman Empire, Morocco had been an outpost on the periphery. In the early years of the Islamic empire, it was likewise only a remote province. Once Islam had incorporated north-west Africa, it soon went its own way, but it was greatly affected by the major political events in the Islamic east, particularly the ideological and religious schisms.
The most important of the eastern political developments was the split between Sunnis and Shiites over who should lead the community after the death of the Prophet. This took place during the period of the initial expansion of Islam out of the Arabian Peninsula, and it was not until the Sunni Umayyads had settled the question (at least in practical terms) and established the caliphate in Damascus in 661 that the real conquest of north-west Africa could begin. In 674, the Muslim commander Uqba ibn Nafi founded a new base at Kairouan, in what is now a southern Tunisia. From there he struck inland, outflanking the Byzantines in their coastal garrisons and reaching the Atlantic coast in 682, where, according to legend, he charged his horse into the surf, crying ‘Oh God! If the sea had not prevented me, I would have coursed on forever like Alexander the Great, upholding your face and fighting all who disbelieved!’ Uqba was killed soon after, and Muslim control collapsed in the face of Berber revolt. In 704, a new province was established at Kairouan, and Musa bin Nusayr, the first governor, set about the real conquest of north-west Africa. By 710, he had taken Ceuta and Tangier. At first, Islamic rule followed roughly the line of the old Roman limes, but the Muslims were less inclined to allow Berber kingdoms autonomy. Musa bin Nusayr set up three sub-provinces, in Tlemcen (modern Algeria), Tangier, and the Sous, a savanna region of southern Morocco. These had Arab governors with small Arab military contingents, but the bulk of their armies were Berber.
Christians were not forced to convert, but most Berbers, who were not Christians, converted willingly. The Berber commander, Tariq ibn Ziyad, the governor of Tangier, led the first Muslim army, which was also largely Berber, across the Strait of Gibraltar in 715, but many Berbers resented the way the Arab elite behaved, treating the Berbers unequally. From early on, Berbers tended to align themselves with heterodox movements.
One such was Kharijism, a fiercely egalitarian movement that believed that religious belief trumped social status, rejected both Sunni and Shiite ideas and prized commitment to Islam above birth or racial or ethnic origin. A Kharijite insurrection in Tangier in 739 or 740 over taxation was defeated, but Kharijis continued to rebel in the mountains, and, in the mid-8th century, set up a Kharijite base at Sijilmasa, in the Tafilalt oasis system of south-western Morocco, to exploit the growing gold and salt trade across the Sahara. This helped spread Islam through southern Morocco and into the Sahara.
Another heterodox movement was entirely home-grown: the Barghawata had its base in the Atlantic plains and seems to have combined elements of Christianity, Judaism, and animism with Shiism. It had its own holy book influenced by the Koran (but written in Berber) and its own prayers and dietary laws; it lasted until the middle of the 11th century.
The most important movement was Shiite and was centred near Volubilis, where Idris ibn Abdullah, who was descended from Ali and Fatima’s son Hasan, found refuge with the local Berber tribe. In 789 he started a small settlement on the banks of the River Fez, and this so concerned the Abbasid caliph Harun al-Rashid that he had Idris poisoned. His infant son succeeded him and made Fez his capital. By the time the son, Idris II, died in 828, he controlled an area from the Rif Mountains to the Sous, made rich by trade.
The Sunni Abbasids, who replaced the Umayyads and ruled from Baghdad, appointed their own governors in Kairouan but never really controlled Morocco, nor did the rump of the Umayyads who settled in Iberia. Fez became a destination for refugees from both places and added to its wealth and intellectual sophistication. The mosques of the Andalusiyin (begun in 857) and the Qarawiyin (859/60) became centres of learning, and the Qarawiyin (also known as al-Karaouine mosque, masjid al-Andalus or Andalusian mosque) claims to be one of the world’s oldest universities.
At the end of the 9th century, a new heterodoxy entered north-west Africa, this time directly from the east, in the form of the Shiite Fatimids, who first spread their propaganda in Sijilmasa and then founded a new dynasty based at Mahdia, in Tunisia. In 972, the Fatimids conquered Cairo – once again, the mainstream had peripheralized Morocco.
Even though Morocco was marginalized politically, it was rich economically from trade across the Sahara, and with al-Andalus. Fez prospered, grew, and was encircled by new walls. Muslims and Jews arrived in large numbers.