Muhammad ibn al-Hanafiyya, born in the year six hundred thirty-seven, was a prominent figure in early Islamic history, recognized as the son of Ali ibn Abi Talib, the fourth caliph in Sunni Islam and the first imam in Shia Islam. During his father's caliphate, which lasted from six hundred fifty-six to six hundred sixty-one, Ibn al-Hanafiyya served as an effective lieutenant, playing a crucial role in the administration and defense of the nascent Muslim community.
Following the tragic assassination of Ali and the subsequent deaths of his sons Hasan and Husayn, Ibn al-Hanafiyya emerged as a significant leader within the House of Ali. His influence grew when Mukhtar al-Thaqafi, claiming to represent him, led a rebellion in Iraq in six hundred eighty-six to avenge the massacre of Husayn and his family by the Umayyad caliph Yazid bin Mu'awiya. Although Ibn al-Hanafiyya did not actively participate in this uprising, he was notably rescued by Mukhtar during a period of detention by the rival caliph Abd Allah ibn al-Zubayr.
Even after the defeat of Mukhtar in six hundred eighty-seven, support for Ibn al-Hanafiyya persisted, particularly through the Kaysanites, a Shia sect that traced its imamate lineage to him and his descendants, especially his son Abu Hashim. Following his death around the year seven hundred, some Kaysanites proclaimed him as the Mahdi, the awaited Islamic leader destined to return and eradicate injustice and evil at the end of times. This belief in his messianic role contributed to the organizational framework that later enabled the Abbasids to successfully overthrow the Umayyads in the year seven hundred fifty.