The Hammadid dynasty (الحمّاديون) was a branch of the Sanhaja Berber dynasty that ruled an area roughly corresponding to north-eastern modern Algeria between 1008 and 1152. The state reached its peak under Nasir ibn Alnas during which it was briefly the most important state in Northwest Africa. Its realm was conquered by the Almohad Caliphate.
The Hammadid dynasty's first capital was at Qal'at Bani Hammad. It was founded in 1007, and is now a UNESCO World Heritage Site. When the area was sacked by the Banu Hilal tribe, the Hammadids moved their capital to Béjaïa in 1090.
In 987 and 989, al-Mansur ibn Buluggin, the emir of the Berber Zirid dynasty, appointed his uncle Hammad ibn Buluggin as governor of Ashir and western Zirid lands. Hammad subsequently defended the territory against Zenata incursions and was granted additional lands by al-Mansur's successor Badis ibn Mansur. In 1007 and 1008, forces under Hammad left Ashir and built a new citadel-capital, Qal'at Bani Hammad (also called Al Qal'a of Beni Hammad), in M'Sila Province in the Hodna Mountains; a thriving city sprung up around the fortress.
In 1014, Hammad declared his independence from Zirid suzerainty and switched his spiritual allegiance from the Shi'a Fatimid caliphs to the Sunni Abbasid caliphs of Baghdad. The Zirids failed to quash the rebellion and recognized Hammadid legitimacy in 1017, in a peace with al-Mu'izz that was sealed by Hammad's son and successor, Qaid ibn Hammad. In 1039 Qaid ibn Hammad (r. 1028–1054) was attacked by Hammama, the ruler of Fes, however Hammama returned to Fez, requested peace and declared his submission to the Hammadids. Al-Mu'izz subsequently also broke with the Fatimids and changed his allegiance to the Abbasids; the Fatimids under al-Mustansir, along with their fierce Bedouin Arab allies, the Banu Hilal and Banu Sulaym, subsequently launched a massive and devastating campaign in present-day Libya, Tunisia, and Algeria, culminating in al-Mu'izz's defeat in 1053 and the subsequent reduction of the Zirids to a small, insignificant territory based in Mahdia.