In 301/914, al-Mahdi founded a new city on the coast near Kairwan and gave to it the name of al-Mahdiya, that served as the Fatimid capital for some generations. The site selected on the Gulf of Gabes, between Susa and Sfax on a small peninsula with a narrow neck just into the sea for nearly a mile in length and less than 500 yards in breath, which terminates the cape of Africa. It was the "town of Africa" of the European historians of the Middle Ages. The landscape of the new city was like a hand stretching out onto the waters of the Mediterranean Sea. There were only two entrances of castles, mosques, fortresses and warehouses and the fortification along the shore consisted of a thick wall barrier. The reflection of light and the imagery of waves on the rocks are unimaginable. There were 16 towers of which 8 belonged to the original foundation and another 8 were added in a later period.
The official inauguration of the new capital was pushed forward to 8th Shawal, 308/February 20, 921. Ibn Idhari al-Marrakushi composed a poem in 308/921 in "Bayan al-Maghrib" (Leiden, 1948, 1st vol., p. 184) for al-Mahdi to celebrate his arrival in the new capital, whose few couplets are given as under:-
Congratulations, O magnanimous prince, For your arrival on which time smiles. It is al-Mahdiya, the sacred, the protected, Just as the sacred places are in Tihama. As if your footprints make it, The Maqam Ibrahim when there is no maqam (station). O Mahdi, Dominion is itself a servant to you, Served by time itself. The world is yours and your progeny's wherever you are. In it all of you will always be Imam.The population soon grew rapidly, therefore, a second city had to be built nearby, to which al-Mahdi gave the name of Zawila.
Mahdiya retained its originality with eye-catching architecture for over 600 years, but it had been decayed by the European ruler. The Spanish historian L. del Marmol Carajal, who was present when the entire fortifications were blown up by Charles V in 1553, vide "Descripcion general de Affrica" (Granda, 1573, 2nd vol., p. 270).