A city already existed at this site during the time of the Phoenicians and Romans; classically it was known as Ruspae and later as Henchir Sbia or just Sbia. The Mahdia shipwreck – a sunken ship found off Mahdia's shore, containing Greek art treasures – is dated to about 80 BC, the early part of Roman rule in this region.
The Muslim Mahdia was founded by the Fatimids under the Caliph Abdallah al-Mahdi in 921 and made the capital city of Ifriqiya, by caliph Abdallah El Fatimi. It was chosen as the capital because of its proximity to the sea, and the promontory on which an important military settlement had been since the time of the Phoenicians.
In 1087 the town was attacked by raiding ships from Genoa and Pisa who burned the Muslim fleet in the harbor. The attack played a critical role in Christians' seizure of control of the Western Mediterranean, which allowed the First Crusade to be supplied by sea. The Zirid dynasty had its residence here in the 11th century, but was brought to an end by the Norman conquest of the city in 1148. In 1160 the city came under Almohad rule.
The role of the capital was taken over by Tunis in the 12th century during the Almohad era, which it remained during the Hafsid Dynasty. Some buildings still exist from the 10th and 11th centuries, such as the Great Mosque and the Casbah, which have helped make the city an important tourist attraction.
Later the city was subject to many raids. In 1390 it was the target of the Mahdian Crusade, when a French army laid siege to the city but failed to take it. Eventually the city was destroyed and burnt down by the Spanish. Though rebuilt, in later times the town lost its logistic and commercial importance.