In Summer 407 AH/1016 AD, a wave of mass violence targeting Isma’ili Shi’i Muslims swept over Ifrīqiyah, a province of the Fatimid caliphate ruled by the Zirid dynasty and consisting of the territories of modern-day Tunisia, western Libya and eastern Algeria. Following their move to Egypt in the late 10th century, the Fatimid caliphs had appointed the Zirids, a dynasty of Sanhaja Berbers, as their governors and deputies in North Africa. Despite occasional outbreaks of violence, for much of the late 10th century there had been a delicate, albeit uneasy, coexistence between the various Muslim communities in Ifrīqiyah (Isma’ili Shi’is, Hanafis, Malikis, and Ibadis). The massacres of 1016 were therefore a cataclysmic set of events that shattered this heterogeneous society.
(Fatimid caliphate at its greatest extent)
The massacres, which began in Qayrawan and spread across Ifriqiyah between 407/1016 and 408/1017, claimed the lives and property of thousands of Isma’ili Shi’is, essentially ending the existence of Isma’ilism as a significant presence in Ifriqiyah, where it had thrived for over a century. Although never a majority of the population, the Isma’ilis were a privileged religious minority whose social status, association with the Fatimid authorities and religious faith made them a target of the (largely Sunni) populace. This violence, which was underpinned by various social, political, economic and religious factors did not occur in a vacuum. The broader context of its occurrence can be seen in the increasing sectarian tensions throughout the Fatimid caliphate during the reign of al-Ḥākim bi Amr-illāh (r. 386/996–412/1021), whose repressive social and religious policies alienated large numbers of his Sunni subjects. Moreover, the increasingly anti-Isma’ili theological discourse among many Sunni communities across the Islamic world, best exemplified by the theological creed issued by the Abbasid caliph al-Qādir (r. 381/991–422/1031) in Baghdad, also played a role in legitimizing such anti-Shi’i violence. In addition, the roots of the violence may be located in the staunch opposition of the Maliki scholars (and their followers) to Isma’ili Shi’ism and the Fatimid regime’s social and religious policies. The massacres in Zirid Ifrīqiyah marked the beginning of the end of Fatimid rule in North Africa and have often been interpreted within the broader context of the Zirids’ formal renunciation of the Fatimids and declaration of allegiance to the Abbasids. As noted by various chroniclers and modern scholars, the massacres and their broader context cannot be separated from the larger process of the establishment of the exclusive hegemony of the Maliki school of thought in North Africa.
(The Zirid emirate around 1000 A.D.)
Unfortunately, very few contemporary sources or eye-witness accounts have survived for this major historical event. Rather, we are forced to rely upon later historical accounts (many of which preserve earlier narrations). Nevertheless, these sources can still tell us much about the broader context and give us some insight into how these events were interpreted by later generations. The following are my translations of three particularly significant accounts by the late medieval Muslim historians ‘Izz al-Dīn ‘Alī ibn al-Athīr (d. 630/1233), Lisān al-Dīn ibn al-Khaṭīb (d. 776/1374) and Abū Zayd ‘Abd al-Raḥmān ibn Khaldūn (d. 808/1406). All three accounts seem to indicate that it was the resurgence of militant Sunnism, especially during the reign of al-Mu ‘izz ibn Bādis (r. 407/1016–454/1062), that motivated the outbreak of the massacres, which are recounted in all their horrific detail. Both Ibn al-Khaṭīb’s and Ibn Khaldūn’s accounts also emphasize that these massacres initiated the Zirid break with the Fatimids, which led the latter to facilitate the invasion of North Africa by the large confederations of the Banū Hilāl and Banū Sulaym tribes, a decision that would have catastrophic consequences for the region.
Translation: Ibn al-Athīr (d. 630/1233), al-Kāmil fï al-Tārïkh
In Muharram 407 AH [July 1016], the Shi’i communities across North Africa were massacred. The reason is as follows: when al-Mu‘izz b. Bādis [r. 407/1016–454/1062] became ruler and arrived in Qayrawan, he was greeted by large crowds of people. After encountering one of these crowds, he asked his advisers about who they were. They replied: “These are Shi’is (rāfiḍah), a group that curses Abū Bakr [ibn Abī Quḥāfah, d. 13/634] and ‘Umar [ibn al-Khaṭṭāb, d. 23/644].” So he proclaimed: “May God be greatly pleased with Abū Bakr and ‘Umar!” Hearing this, [mobs of] common people attacked the neighborhood where large groups of Shi’is would gather, and slaughtered them all. The troops and their auxiliaries also supported and participated in these attacks due to their desire for the wealth that was to be plundered. The common folk unleashed their violence against the Shi’is, encouraged and incited further by the governor of Qayrawan.
(Great Mosque of Qayrawan, one of the oldest mosques in North Africa)
The reason that the governor of Qayrawan encouraged this violence was because he had heard that, despite his competence in maintaining order, al-Mu‘izz b. Bādis sought to remove him from office. During this outbreak of violence, large numbers of Shi’is were murdered, burned alive, and their homes were plundered. These massacres occurred throughout Ifriqiya [modern-day Tunisia, eastern Algeria and western Libya]. A large group of survivors, seeking refuge, fortified themselves in the palace of al-Manṣūr near Qayrawān, but were soon besieged by large mobs until they were on the brink of starvation. When they attempted to escape, the people slaughtered every single one of them. Even those who sought refuge in the Great Mosque of al-Mahdia were all murdered. In North Africa, the Shi’is were usually referred to as “Easterners” (al-mashāriqah), based on the eastern origin of Abū ‘Abd Allāh al-Shī‘ī [d. 298/911]. Most of the poets have mentioned this event, with some celebrating it with happiness while others lamenting it with tears.
[‘Izz al-Dīn ‘Alï ibn al-Athīr, al-Kāmil fï al-Tārīkh (Beirut, 2008), Vol. 9, pp. 294–295]
(Great Mosque of Mahdia)
Translation: Ibn al-Khaṭīb (d. 776/1374), A‘māl al-A‘lām
It was during the reign of [the Fatimid caliph] al-Mustanṣir [r. 427/1036–487/1094] that Abū Tamīm al-Mu‘izz [b. Bādis, r. 407/1016–454/1062]—one of the kings of the Ṣanhājah [Berbers] renounced his allegiance to the Fatimid dynasty, proclaimed the Friday sermon in the name in the name of the Abbasid caliph al-Qā’im [r. 422/1031–467/1075], and ordered the public cursing (la‘n) of the Fatimids in the mosques. When al-Mustanṣir learned of this, it greatly worried and saddened him. When his vizier, [Abu’l-Qāsim ‘Alī ibn Aḥmad] al-Jarjarāʾī [vizier from 418/1027–436/1045], who was among the shrewdest and most cunning of individuals, saw him distraught he asked: “What saddens you, O Commander of the Faithful?”
[al-Mustanṣir] replied: “How can I not be saddened?! Our dominion in North Africa has vanished, Muslims have been massacred in Qayrawan, and the name of our enemy has been praised from our own pulpits!”
[al-Jarjarāʾī] responded: “Do not be troubled. Retaliate against them by allowing the fierce Arab tribes of the Banū Hilāl, Zughbah and Riyāḥ, who are located between the Nile and the sea, to cross into North Africa.”
Heeding this advice, al-Mustanṣir wrote to these tribes, praising them greatly and stating: “I hereby give you lordship and authority over the entirety of North Africa and all the realms ruled by the treacherous slave al-Mu‘izz b. Bādis al-Ṣanhājī, so you shall never want for anything again.”
The Arab tribes greedily seized upon this opportunity and invaded North Africa, spreading across the land like locusts and destroying everything in their path until they reached Qayrawan. It was there that al-Mu‘izz b. Bādis engaged them in battle and was terribly defeated. As a result of their presence, those lands have remained unstable and difficult to rule until this very day.
Al-Mu izz Abū Tamīm was the first [Zirid emir] to renounce the Fatimids and proclaim his allegiance for the Abbasids. He ordered the removal of their names from the coinage in 441 AH  and instead had inscribed “If anyone desires a religion other than Islam it will never be accepted of him and in the Hereafter he will be from among the losers” [Q. 3: 85]. When al-Mustanṣir, the ruler of Egypt, and his vizier al-Jarjarāʾī learned of this, they facilitated the crossing of the Arab tribes into Ifrīqiya, something none of their predecessors had permitted. Al-Mustanṣir proclaimed: “By God, I will send against him armies that will only bring him hardship and trouble.” As such, he issued a proclamation to the Arabs and allowed them to cross the Nile into North Africa, which had previously been forbidden to them, and even gifted pelts and gold coins to those who decided to do so. A massive host crossed into North Africa, becoming a source of turmoil and great hardship on al-Mu‘izz and every ruler after him until the present day. These Arab tribes enslaved the lands they conquered, orphaning children, plundering everything, and were able to violently conquer and brutally sack the city of Qayrawan. Until this very day, they have brought those lands only misery with the fertile plains being reduced to impoverished wastelands.
[Ibn al-Khaṭīb, A‘māl al-A‘lām (Beirut, 2003), Vol. 1, p. 247; Vol 2., p. 324]
Translation: Ibn Khaldūn (d. 1406), Kitāb al-‘Ibar
Al-Mu‘izz b. Bādis was a major opponent of Shi’ism (madhāhib al-rāfiḍah) and a staunch adherent of Sunnism. From the beginning of his reign, he openly proclaimed his Sunni identity and publicly cursed the Shi’is. Shortly thereafter, he ordered the massacre of every Shi’i in his realm. The reason for this is that one day he had fallen off his horse and invoked the names of Abū Bakr and ‘Umar in his supplication for divine aid, which motivated the common folk to attack the Shi’is, massacring them in the most brutal way and even killing the leader of their community. The Shi’i caliph in Cairo was extremely displeased by this.
(Gold dinar of Fatimid caliph al-Mustansir)
The Fatimid vizier Abu’l-Qāsim [‘Alī ibn Aḥmad] al-Jarjarāʾī [vizier from 418/1027–436/1045] wrote to al-Mu‘izz warning him about the consequences of his actions. Al-Mu’izz replied harshly with insults against the Fatimid caliphs which led to a worsening of the relationship. This deterioration of relations continued until al-Mu’izz formally renounced his allegiance to the Fatimids in 440 AH  during the reign of the Fatimid caliph al-Mustanṣir [r. 427/1036–487/1094]. Al-Mu’izz had the Fatimid banners publicly burned, the caliph’s name removed from all coins and royal robes, and proclaimed the allegiance of his realm to the Abbasid caliph al-Qā’im b. al-Qādir [r. 422/1031–467/1075]. Al-Qā’im then sent Abū al-Faḍl b. ‘Abd al-Wāḥid al-Tamīmī to [al-Mu’izz] with an official investiture [recognizing his realm as part of the Abbasid caliphate]. In retaliation, the Fatimid caliph al-Mustanṣir sent against him the Hilāl confederation of Arab tribes—who had previously belonged to the Qarāmiṭa—which included the tribes of Riyāḥ, Zughba, Athbaj. This was done with the encouragement of the Fatimid vizier Abū Muḥammad al-Ḥasan b. ‘Alī al-Yāzūrī [vizier from 442/1050 to 450/1058], as we have previously mentioned in our discussion of the history of the Arabs and their arrival in Ifrīqiyah.”
[Abū Zayd ‘Abd al-Raḥmān ibn Khaldūn (d. 1406), Kitāb al-‘Ibar (Beirut, 2010), Vol. 6, p. 169–170]
I don’t think we can say the Fatimids controlled Morocco, as the map implies. The area was for most of the 10th century a contested zone between the Fatimid and Córdovan Caliphates, and at the end of the century most of northern Morocco was integrated into a province of the Umayyad Caliphate by the “hajib” (“chamberlain”, the highest post in the Umayyad bureaucracy, close to the eastern Viziers) Almanzor.