There are numerous Prophetic traditions about the Mahdi reported by both the Sunni and the Shia sources. Close examination of the contents of these traditions proves that the subject of the future coming of the Mahdi and the Qaem was a well-established tenet during the Prophet’s life time. People anticipated someone who would take upon himself to establish truth and spread the worship of God. Moreover, they expected that person to take charge of purifying the earth and instituting justice. The belief was so wide spread among the people that having verified it in principle they were engaged in discussing its details.
Sometimes they would ask: "From which family would the awaited Mahdi arise?" At other times they wanted to know his name and patronymic. Still at other times they wanted to know the reason why he was called the Mahdi. They wanted to know about his revolution and asked about the signs of his appearance. They also wanted to find out if the Mahdi and the Qaem were one and the same person. They were told about the Mahdi’s occultation and wanted to understand the reasons and the obligations of his followers while he was in occultation.
The Prophet also from time to time used to inform people about the existence of the Mahdi...
The Prophet also from time to time used to inform people about the existence of the Mahdi. He would inform them saying: "Mahdi will be among my descendants. He will be among the sons of Fatimah among the descendants of Hussein." At other times he would announce his name and patronymic and give information about the signs of his reappearance and other related matters.
The Discussion among the Companions and the Subsequent Generations:
After the Prophet’s death the story of the coming of the Mahdi was often heard among the prominent companions of the Prophet and the following generation. The matter was regarded among the religious truths and was treated as one of the certain future events. The following are some examples of this in the sources:
Abu Hurayra says: "People will pay allegiance to the Mahdi between rukn and maqam." Ibn Abbas is reported to have told Mu’awiya that a person among the descendants of the Prophet will rule for forty years at the End of Time. On another occasion a man asked Ibn Abbas to inform him about the Mahdi. He said: "I hope that in the near future a young man from our family (the Hashimite) will arise to put an end to civil strife and sedition." Ibn Abbas also specified the descendant of the Prophet as being from the children of Fatimah. According to another famous companion of the Prophet Ammar Yasir: "At the time when Nafs al-Zakiyya is killed a caller from the heaven will say: 'your commander is so-and-so.' Following it the Mahdi will emerge and fill the earth with justice and equity."
Abd Allah bin Umar mentioned the name of Mahdi in the presence of an Arab who said: Mahdi is Mu’ awiya bin Abu Sufyan. Abd Allah said: "It is not as you say. Mahdi is a person behind whom Jesus will offer his prayers."
Umar bin Qays asked Mujahid if he knew anything about the Mahdi since he did not believe in what the Shi’ a were saying about him. Mujahid said: "Yes I do. One of the Prophet’s companions told me that the Mahdi will not appear until that time when Nafs al-Zakiyya will be killed. At that time, he will take the command and will fill the earth with justice and equity."
Nufayl’s daughter Umayra narrates that she heard Hassan bin Ali’s daughter saying: "This affair about which you are waiting will not occur until among you some seek to distance themselves from the others and curse each other."
The author of Maqatil al-talibiyin Abu al-Faraj Isfahani writes that Fatimah Hussein bin Ali’s daughter used to engage in midwifery as a voluntary service to the women of Banu Hashim. Her son used to object to her saying: "We are afraid that you will be recognized as a professional midwife." In reply she would say: "I am awaiting someone. As soon as he is born I will stop assisting in delivery."
Qatada asked Ibn Musayyib: "Is the existence of Mahdi a truth?" He said: "Yes. He is a member of the Quraysh among the descendants of Fatimah." A similar tradition is reported from the famous scholar Zuhri who also related that the Mahdi will be among the descendants of Fatimah. Abu al-Faraj reports an event when Walid bin Muhammad was with Zuhri and a clamor transpired. Zuhri asked Walid to find out what had caused it. After finding out Walid reported: "Zayd bin Ali has been killed and his head has been brought." Zuhri was upset and said: "Why is this family in haste? Haste has destroyed a number of them." Walid asked: "Will they reach power?" He replied "Yes because Ali bin Hussein narrated to me on the authority of his father who heard this from Fatimah the Prophet’s daughter who in turn heard the Prophet tell her: 'Mahdi will be among your descendants.'"
Other sources that mention these traditions include the following:
Ibn Sirin used to say that the promised Mahdi will be from this Umma. He will be the one who will lead Jesus in prayers. 
In another place he reports a tradition from Abd Allah bin Harith. He said: "The Mahdi will arise at the age of forty and will resemble the Children of Israel." A variant of this tradition reported by Artat says that the Mahdi will arise at the age of twenty. Another tradition in the same section explains the reason Mahdi was named thus. Ka’b says: "He was named Mahdi because he will be guided to the hidden matters." Abd Allah bin Shurayk used to relate that the Prophet’s standard will be with the Mahdi. 
Ibn Sirin records several other traditions that speak about the function of the Mahdi. One of these reported from Hakam bin Uyayna says that the reporter asked Muhammad bin Ali al-Baqir: We have heard that one among your Ahl-ul-Bayt will arise and will establish justice and equity. Is this true? He said: "We are also awaiting his appearance and living in hope."
In another tradition Salma bin Zafar reports:
One-day people were talking about the appearance of the Mahdi in the presence of Hudhayfa. Hudhayfa said: "
If Mahdi has indeed appeared while you are living close to the Prophet’s period and while his companions are living among you then you are truly fortunate. However, that is not the case. Mahdi will not appear until people are devoured by oppression and tyranny and there is no one absent more beloved and more needed than him."
People were so familiar with the characteristics of the Mahdi that Jarir the Arab poet read the following lines of his poem for the Umayyad caliph Umar bin Abd al-Aziz in which he compares the caliph with the future Mahdi:
Your presence is a blessing. Your conduct is the conduct of the Mahdi. You are fighting your lower self and you spend the night in recitation of the Qur’an. 
Muhammad bin Ja’far reports that he once told Malik bin Anas his misfortunes. He said: "Wait until the significance of the verse of the Qur’an: 'Yet We desired to be gracious to those that were abased in land and to make them leaders and to make them the inheritors (27:5) ' becomes materialized."
(1). Ibn Tawus Kitab al-malahim wa al-fitan p. 64. Rukn and maqam are two sacred spots in the grand mosque of Mecca.
(2). Ibid. p. 84.
(3). Ibid. p. 179.
(5). Ibid. p. 171.
(6). Majlisi Bihar al-anwar Vol. 52 p. 211.
(7). Maqatil al-talibiyin p. 160.
(8). Kitab al-hawi li al-fatawa Vol. 2 p. 135.
(9). Ibid. p. 147-150.
(10). Ibid. p. 159.
(11). Ibn Qutayba al-Imama wa al-siyasa Vol. 2 p. 117.
(12). Maqatil al-talibiyin p. 359.
Source: A book named The Just Leader of Humanity
By Ayatullah Ibrahim Amini
Translated by: Dr. Abdulaziz Sachedina