The idea of the final victory of the forces of righteousness, peace and justice over those of evil, oppression and tyranny, of the world-wide spread of the Islamic faith, the complete and all-round establishment of high human values, the formation of a utopian and an ideal society and lastly the accomplishment of this ideal at the hands of a holy and eminent personality called, according to the Islamic traditions, Mahdi is a belief which, of course with variations in details, is shared by all the Muslim sects and schools of thought.
Basically this is a Qur'anic concept and it is the holy Qur'an which in very clear terms, predicts:
1. The final victory of Islam.
It is He who has sent His messenger with the guidance and the religion of truth to make it prevail over every other religion. However much the disbelievers may dislike it. (Surah al-Tawbah, 9:33 and Surah As-Saff, 61:9)
2. The absolute supremacy of the good and the pious.
Indeed We have written in the Psalms after the Torah had been revealed: The righteous among My slaves shall inherit the earth". (Surah al-Anbia. 21:105)
3. The final collapse of the oppressors and the tyrants.
We willed to show favour to those who were persecuted in the earth and to make them leaders and masters. It was also Our will to give them power in the earth and to show Pharaoh, Haman and their hosts to experience from their victims what they feared most. (Surah al-Qasas, 28:5-6)
4. A bright and happy future for humanity.
Moses told his people to seek help from Allah and exercise patience. The earth belongs to Him and He has made it the heritage of whichever of His servants He chooses. The Final Victory is for the pious. (Surah al-A'raf 7:128)
This idea is not an outcome of any wishful thinking, but it emanates from the total working of the system of nature, the evolutionary process of history, man's confidence in the future and the total rejection by him of pessimism about the destiny of mankind, which is extraordinarily bleak, according to certain theories.