With the death of Husayn, Muhammad's grandson, in Karbala, the first attempt to put an Alid (a descendant of Ali) in control over the Islamic empire had failed. Their supporters continued to challenge the Umayyads who had emerged victorious and succeeded in gaining power over the entire Islamic world. Ali's cause did not come first for all supporters of these rebellions. Many had other reasons for opposing the Umayyads, such as the Umayyad favoritism for their family, Syria, and the Arabs; the Alids offered the most obvious alternative.
Kufa in particular became a center for resistance. The historian Heinz Halm sees the beginnings of Shiism as a religious community in this region in the 680s when a movement of 'penitents' (Arabic: tawwabun) appeared who demanded revenge for Husayn and wanted to atone with swords in their hands for having let him down.Initially a movement of a small number of elderly men, it turned into a rebellion with broader support, but it was crushed by the Umayyads in 685. Eventually this public display of solidarity and grief would become a distinctive feature of Shiite public religiosity.
Iraq during the formative period is also the backdrop against which Shiism developed one of its most distinctive features, the concept of the Hidden Imam. Unlike Sunni Islam, where the term imam commonly denotes the leader of prayer, in Shiism the Imam is a descendant of Ali and seen as the politico-religious leader of the entire community. A line of historical figures who were highly respected scholars and later identified as Imams was put on hold when one of them (Shiite sects disagree about the point in the line where this took place) went into Occultation (hiding). One of the aspects of this crucial figure is its apocalyptic character. For many Shiites, the Hidden Imam is the Mahdi who appears at the end of time and restores justice. He is also referred to as al-Qaim, 'he who arises'. Sometimes a difference is made between mahdis who temporarily restore justice and the Mahdi-Qaim who ushers in the final period of grace.
While Sunnis share the belief in such a figure (although he is not expected to take revenge for the injustice the Shiites suffered), the appearance of mahdis as leaders of political movements is usually associated with Shiism, although the leaders were not necessarily Shiites. The rebellion of the Sudanese Mahdi against the Turco-Egyptian government in the 1880s is a well-known example of a Sunni Mahdi. In post-invasion Iraq, one of the most powerful forces in the country is called the Mahdi army (led by the Shiite Moktada al-Sadr). The Mahdi is also mentioned in the constitution of the Islamic Republic of Iran. The earliest attested case in Islamic history in which Mahdism played such a role is the rebellion of Mukhtar in Kufa.
In 685, Mukhtar, an Arab, had started his rebellion in the name of Muhammad ibn al-Hanafiyya who, Mukhtar claimed, was the Mahdi. Ibn al-Hanafiyya dissociated himself from the revolt. It is noteworthy that though he was a son of Ali, his mother was not Fatima, but rather a slave. Unlike the men who became later the Shiite Imams, he was thus not a descendant of Muhammad, which indicates an early tendency to highlight Ali as an ancestor rather than Muhammad.
One of Mukhtar's slogans was vengeance for Husayn. He challenged not only the Umayyad caliph Abd Malik, who had only just succeeded to the throne, but also his strong rival Abdallah ibn al-Zubayr in Mecca, who claimed the caliphate for himself. Supporters of Mukhtar included the population of Kufa, who might have felt guilty for having denied Husayn their support, as well as non-Arab converts to Islam. In 687, the rebellion was suppressed by the troops of Ibn al-Zubayr. In the aftermath of these events and the death of Ibn al-Hanafiyya in 700, supporters of the Alids continued to pin their hopes on this elusive Mahdi, who, they claimed, was hiding in a cave only to return and fill the world with justice. In the following years similar stories were told about other leaders of rebellions. The belief in a redeemer hiding either in the material world or a remote metaphysical sphere became one of the most characteristic features of Shiism, although it is not key to the beliefs of many modern Shiites.