Despite the fact that Imam Ghazali’s al-Iqtiṣād fi al-I‘tiqād is a book on beliefs (‘aqīda), he includes a whole chapter on the imamate (caliphate) in this work. At the beginning of this chapter, he includes a discussion of what he describes as ‘definite legal proof’ (al-burhān al-qaṭ‘ī al-shar‘ī) that appointing an imam is an obligation. In this discussion, he also says that ‘religion (dīn) and sultan are twins’:
“We should not think that this obligation derives from the intellect. We have explained that obligations derive from the revelation, except when ‘obligatory’ is interpreted to designate an act, such that there is benefit in performing it or harm in refraining from it. According to this interpretation, it cannot be denied that appointing an imam is obligatory, since it leads to benefit and prevents harm in this worldly life. However, we present a conclusive legal demonstration [البرهان القطعى الشرعى] that it is obligatory. We will not rely solely on the consensus [ijmā‘] of the Muslim community; rather we bring attention to the basis of this consensus.
Hence we say:
Well ordered religious affairs are decidedly a purpose of the man with the revelation [Muhammad] (ﷺ). This is an unquestionable premise about which no dispute is imaginable. We add to it another premise, which is that well-ordered religious affairs can only be achieved through an imam who is obeyed. The correctness of the proposition that the appointment of an imam is obligatory follows from these two premises.
If it is said that the last premise, which is that well-ordered religious affairs can be achieved only through an imam, is not conceded, then we say: “Its demonstration is that well-ordered religious affairs can be achieved only by well-ordered worldly affairs and well-ordered worldly affairs can be achieved only by an imam who is obeyed.” These are two premises: which one is the subject of dispute?
It might be said: “Why do you say that well-ordered religious affairs can be achieved only through well-ordered worldly affairs? On the contrary, it can be achieved only by destruction of worldly affairs, for religious affairs and worldly affairs are opposites, and hence to be occupied with making one of them flourish is the ruin of the other.”
This is the argument of someone who does not understand what we intend here by ‘worldly affairs’. For it is an ambiguous term that may be used to designate luxury and pleasure and being excessive beyond what is needed and necessary, or it may be used to designate all that is required prior to one’s death. One of the designations is opposed to religion and the other is its very condition. It is this way that the one who does not distinguish between the meaning of ambiguous terms errs.
We thus say:
Well-ordered religious affairs are achieved through knowledge and worship. These cannot be achieve without the health of the body, the maintenance of life, the fulfillment of needs – such as those for clothing, shelter and food – and security from the onset of calamities. How true this is: “When a man wakes up safe among his family, with a healthy body, and in possession of his daily sustenance, it is as if the whole world is made available to him.” A man does not achieve security in his life, body, wealth, home, and sustenance under all circumstances but [only] under some. Religious affairs cannot flourish unless security is achieved in these important and necessary matters. Otherwise, if one spends all his time being occupied with protecting himself against the swords of oppressors, and with winning his sustenance from exploiters, when would he find time for working and seeking knowledge, which are his means for achieving happiness in the hereafter? Therefore well-ordered worldly affairs – I mean the fulfillment of needs – are a condition for well-ordered religious affairs.
As for the second premise, which is that worldly affairs and security in life and wealth can be maintained only through an imam who is obeyed, it is confirmed by observing the periods of social upheavals when the sultans and imams die. If these periods are prolonged and not quickly terminated by the appointment of another sultan who is obeyed, the killing would continue and the sword would dominate, famine would spread, livestock would diminish, and industry would collapse; and whoever wins would plunder; and no one who manages to stay alive would have time to worship or seek knowledge; and the majority would die under the shadows of the swords. For this reason it has been said that religion [dīn] and sultan are twins, and also that religion is a foundation and the sultan is a guard: that which has no foundation collapses and that which has no guard is lost.
In sum, no rational person doubts that if mankind, given their different classes, diverse desires, and disparate opinions, are left to their own devices without decrees that they obey and that unify their factions, they would all end in ruin. This is an epidemic that has no remedy other than a strong sultan who is obeyed and who unifies their disparate opinions. This shows that a sultan is necessary for achieving well-ordered worldly affairs, and well-ordered worldly affairs are necessary for achieving well-ordered religious affairs, and well-ordered religious affairs are necessary for achieving happiness in the hereafter, which is decidedly the purpose of all the prophets. Therefore, the obligation of appointing an imam is among the essential requirements of the law [ضروريات الشرع] – a requirement that by no means can be ignored.”
Later on in the chapter, Imam Ghazali argues that necessity dictates that in some circumstances a caliph who does not fulfill all the conditions (as was the case in his time) should still be recognized and obeyed, because of the dire consequences of not having an imam:
“I wish I knew how someone who does not accept this [principle] could judge that the imamate in our time is invalid insofar as its conditions are not fulfilled, while he is unable to replace the imam with someone who seeks it, for even he cannot find someone who fulfills its conditions. Which of his states is better; to say that the judges are dismissed, appointments are invalid, marriages are annulled, all the decrees of the governors everywhere in the world are unenforceable, and all of mankind are [on the verge of] engaging in what is unlawful [ḥarām]; or to say that, based on the current state and necessity, the imamate is valid and the decrees and appointments are enforceable?”
 This is a ḥadīth. It is reported by Ibn Maja, Sunan, XXXVII.9, No. 414; and Tirmidhi, al-Jāmi‘ al-Ṣaḥīḥ, XXXVII.34, No. 2347
[Al-Ghazali’s Moderation in Belief: Al-Iqtiṣād fi al-I‘tiqād, translated by A M Yaqub, Unviersity of Chicago Press, Chicago and London, 2013, pp. 229-231 and 234 (some words in square brackets have been added)]