In 447/1055, the Turk, Tughril Beg was recognized in Baghdad as the sultan and lieutenant of the Abbasid caliph. He drove away the Iranian soldiers from Baghdad to Syria. They assembled round Abu Harith al-Basasari, who was propagating the Fatimid mission. Meanwhile, Ebrahim Niyal rebelled in Mosul against Tughril Beg, who himself set out to crush the revolt. The absence of Tughril Beg from Baghdad gave a chance to al-Basasari to advance and capture Baghdad, which he did successfully in 450/1058 and recited the Fatimid khutba in the cathedral mosque of Baghdad. He also sent the royal throne, robes, pulpit and the staff to al-Mustansir in Cairo. The expelled Abbasid caliph took refuge with an Arab amir for one year.
After subduing the rising of his brother, Tughril Beg turned back to Baghdad with a large army. When he reached near Baghdad, al-Basasari did not come into confrontation, and began to evacuate the city on other side with his close associates. Tughril Beg thus entered the city without any opposition and reinstated the Abbasid caliphate after a year on 6th Zilkad, 451, December 14, 1059. He sent a detachment to pursue al-Basasari, who was slain in the ensuing fighting.
Maghrib was the original abode and the base of the foundation of the Fatimid Caliphate, whose chief in the time of al-Mustansir was al-Muizz bin Badis, the fourth Zirid ruler. He was a Malikite and persecuted the Shiites. It is also related that the relations between him and the Fatimid vizir were strained, whereupon in 436/1044, al-Muizz bin Badis proclaimed Malikism in Maghrib, and recited the Abbasid khutba from 440/1048, resulting the whole Maghrib gone away from the Fatimid occupation in 442/1050.
It is related that al-Muizz bin Badis returned briefly later on in 446/1055 to the allegiance of the Fatimids. In the meantime, the vizir Yazuri had convinced al-Mustansir that he would punish the disloyal al-Muizz bin Badis. Thus, the vizir encouraged a number of bedouin tribes to advance towards Maghrib. The bedouins at the command of Banu Hilal and Banu Sulaym, took possession of Barqa and proceeded into the territories of the Maghrib. They inflicted defeat to the Zirids in 443/1052 and pillaged the towns and gained rich booty. These bedouins, being reinforced by new arrivals, gradually penetrated Maghrib, whose operation is known as the Hilali Invasion. In 449/1057, al-Muizz bin Badis had to evacuate his capital, Kairwan and sought refuge in Mahdiya, then governed by his son, Tamim bin al-Muizz (454- 501/1062-1108). In sum, the Zirids were divided into petty rules in Maghrib. The last Zirid ruler, al-Hasan bin Ali was driven out of Mahdiya in 543/1148 by Roger II, the Sicilian emperor.
It must be known that the Karakhanid dynasty sprang from the ruling house of the Karluk Turks who originally belonged to the steppes of Central Asia, and whose founder was Satuk Bughra Khan. He embraced Islam and assumed the Islamic name Abdul Karim. He reigned from Kashghar and Talas over the western wing of his people. His grandson Hasan Bughra Khan occupied for a while the Samanid capital of Bukhara, which was taken over by Ilig Nasr of Ozkend in 389/999. The Fatimid dais had continued their mission in Bukhara, Samarkand and western Farghana. In 436/1045, a bulk of the converted Ismailis, who recognized the Imamate of al-Mustansir, had been killed in the territories of the Karkhanid rule, impelling the dais to adopt strict taqiya.
In 482/1089, Ahmad Khan bin Khizr (473-482/1081-1089), another Karakhanid ruler of Bukhara, Samarkand and western Farghana, was accused by the Sunni zealot, called Abu Tahir bin Aliyyak, of having embraced Ismailism. He had been deposed and executed due to the hootest opposition of the ulema.