Muhammad II al-Mahdi (Arabic: محمد المهدي بالله, romanized: Muḥammad al-Mahdī bi-ʾllāh) was the fourth Caliph of Córdoba of the Umayyad dynasty in Al-Andalus (Moorish Iberia).
In early 1009, Muhammad II, an Umayyad prince and pretender to the throne of Córdoba, rose to power, led a rebellion against the existing Caliph, ultimately deposing and imprisoning Hisham II. Muhammad II then took the throne for himself but oppressed, slaughtered, and ultimately expelled many resident Berbers from the city. Great numbers of alienated Berbers traveled north to Calatrava where they regrouped, created a fighting force and selected Sulayman ibn al-Hakam as their candidate for caliph.