Muḥammad ibn al-Ḥasan al-Mahdī (محمد بن الحسن المهدي) is believed by the Twelver Shia to be the last of the Twelve Imams and the eschatological Mahdi, who will emerge in the end of time to establish peace and justice and redeem Islam.
Hasan al-Askari, the eleventh Imam, died in 260 AH (873-874 CE), possibly poisoned by the Abbasids. Immediately after his death, his main representative, Uthman ibn Sa'id, claimed that the eleventh Imam had an infant son named Muhammad, who was kept hidden from the public out of fear of Abbasid persecution. Uthman also claimed to represent Muhammad, who had entered a state of occultation. Other local representatives of al-Askari largely supported these assertions, while the Shia community fragmented into several sects over al-Askari's succession. All these sects, however, are said to have disappeared after a few decades except the Twelvers, who accept the son of al-Askari as the twelfth and final Imam in occultation.
Uthman was followed by three more agents, collectively known as the Four Deputies, who were regarded by the Twelver community as representatives of Muhammad al-Mahdi. This period, later termed the Minor Occultation, ended after about seventy years with the death of the fourth agent, Abu al-Hasan al-Samarri (940-41). He is said to have received a letter from Muhammad al-Mahdi shortly before his death. The letter predicted the death of Abu al-Hasan in six days and announced the beginning of the complete occultation, later called the Major Occultation, which continues to this day. The letter, ascribed to Muhammad al-Mahdi, added that the complete occultation would continue until God granted him permission to manifest himself again in a time when the earth would be filled with tyranny.
The Twelver theory of occultation crystallized in the first half of the fourth (tenth) century based on rational and textual arguments. This theory, for instance, sets forth that the life of Muhammad al-Mahdi has been miraculously prolonged, arguing that the earth cannot be void of the Imam as the highest proof of God.