The necessity of Allah’s (swt) grace, the existence of a khalifatullah and vicegerent of Allah (swt) and his protection from any calamities and bad incidents can be counted as a portion of the theological basis of Mahdiism.
A thorough analysis of the theological basis of Mahdiism (the belief in Imam Mahdi as and his coming) calls for a detailed and profound discussion which we will leave for another time. Here, we will briefly mention a portion of the basis of this concept and belief:
Great theologians link the concept of Mahdiism to the “Law of Grace” which is a famous one in theology and have proven the necessity of the existence of an imam through this law. Khajeh Nasiruddin Tusi, a great Muslim theologian says: انحصار اللطف فیه معلوم للعقلاء و وجوده لطف و تصرفه آخر و عدمه منا , meaning that it is manifest to the wise, that the grace of Allah (swt) (after sending the prophet) solely lies in Him appointing an imam so that the people don’t remain without a leader and teacher and one to train them religiously and so that the book (the Quran) and tradition (the hadiths) have one to interpret and explain them, and so that God’s grace continues to flow through him to the rest of creation. The imam’s existence per se is grace and mercy, the things he does regarding different issues and his presence in society, completing the Islamic government, the spreading of Quranic teachings by him are all cases of Allah’s (swt) grace and mercy upon the people, and his concealment and occultation is our own fault.
It has been stated in the book Kifayatul-Muwahhidin that since Imam Mahdi (as) was the last imam and “proof” of Allah (swt), it was necessary for him to be protected from all calamities and dangers that threatened him and remain hidden by Allah (swt). Since the imam’s situation is like that of the Prophet’s (pbuh) after the deliverance of all Islamic rules and laws, there is nothing wrong with his concealment, even though such a matter deprives the people of seeing him. Therefore, it is an obligation for all to practice Islam and abide by all of its rules even though his excellency isn't present at the moment. Of course, if some day all of Islam’s rules are lost and nothing remains of it, Imam Mahdi (as) will immediately reappear.
For further information:
1- Kashful-Murad, pg. 363.
2- Sharh Tajrid, pg. 376.
3- Kifayatul-Muwahhidin, vol.3.
4- Bayanul-Irfan, vol.5.