Praise be to Allah.
Earthquakes are one of the great signs of Allaah in this universe, with which He tests His slaves as a reminder or to instil fear of Him or as a punishment. Man must remember, when the signs occur, his weakness, incapability, humble status before Allaah and his need of Him, so he turns to Allaah, may He be exalted and glorified, in supplication, beseeching and humility, in the hope that Allaah will lift this great distress from the people.
Allaah says (interpretation of meaning):
“Verily, We sent (Messengers) to many nations before you (O Muhammad صلى الله عليه وسلم). And We seized them with extreme poverty (or loss in wealth) and loss in health (with calamities) so that they might humble themselves (believe with humility).
43. When Our Torment reached them, why then did they not humble themselves (believe with humility)? But their hearts became hardened, and Shaytaan (Satan) made fair‑seeming to them that which they used to do.
44. So, when they forgot (the warning) with which they had been reminded, We opened for them the gates of every (pleasant) thing, until in the midst of their enjoyment in that which they were given, all of a sudden, We took them (in punishment), and lo! They were plunged into destruction with deep regrets and sorrows”
Hence the fuqaha’ (may Allaah have mercy on him) regarded it as mustahabb to offer a great deal of prayers for forgiveness and supplications, to beseech Allaah and to give charity when earthquakes happen, as is also mustahabb when solar and lunar eclipses occur.
The great scholar Zakariya al-Ansaari (may Allaah have mercy on him) said:
It is mustahabb for everyone to beseech Allaah in supplication and so on when earthquakes and similar events, such as thunderbolts and strong winds, happen, and to offer prayers on his own in his house lest he be negligent, because the Prophet (blessings and peace of Allaah be upon him) said, when the wind blew strongly: “O Allaah, I ask You for its good and the good of what is in it and the good of what it is sent with, and I seek refuge in You from its evil and the evil of what is in it and the evil of what it is sent with.” Narrated by Muslim. End quote.
Asna al-Mataalib Sharh Rawd al-Taalib (1/288). See also Tuhfat al-Muhtaaj (3/65).
See also the answer to question number 2593.
But there is no evidence in the Sunnah -- as far as we know -- to suggest that there is a particular dhikr or du’aa’ that it is mustahabb to recite when an earthquake occurs. Rather one should offer du’aa’ as inspired by Allaah to ask for mercy and help from Allaah, may He be exalted and glorified, so that He might divert this calamity from the people.
Shaykh Ibn Baaz (may Allaah have mercy on him) said:
What must be done at the time of an earthquake or other calamity, or an eclipse, strong wind or flood is to repent to Allaah, may He be glorified, and to beseech Him and ask Him for well-being, and to remember Him a great deal and ask Him for forgiveness, as the Prophet (blessings and peace of Allaah be upon him) said at the time of an eclipse: “When you see that, then turn to remembrance of Allaah and call upon Him and ask for His forgiveness.” Agreed upon. It is also mustahabb to show compassion to the poor and needy, and to give charity to them, because the Prophet (blessings and peace of Allaah be upon him) said: “Show mercy, you will be shown mercy.” (Narrated by Ahmad) And he said: “Those who show mercy will be shown mercy by the Most Merciful. Show mercy to those who are on earth and the One Who is in heaven will show mercy to you.” Narrated by al-Tirmidhi. And he (blessings and peace of Allaah be upon him) said: “The one who does not show mercy will not be shown mercy.” Narrated by al-Bukhaari. It was related from ‘Umar ibn ‘Abd al-‘Azeez (may Allaah have mercy on him) that he used to write to his emirs when earthquakes took place telling them to give charity.
One of the means of being safe and sound from all evils is for those in authority to hasten to restrain the foolish and compel them to adhere to the truth, and to implement the laws of Allaah among them, and to enjoin what is good and forbid what is evil, as Allaah says (interpretation of the meaning):
“The believers, men and women, are Awliyaa’ (helpers, supporters, friends, protectors) of one another; they enjoin (on the people) Al‑Ma‘roof (i.e. Islamic Monotheism and all that Islam orders one to do), and forbid (people) from Al‑Munkar (i.e. polytheism and disbelief of all kinds, and all that Islam has forbidden); they perform As-Salaah (Iqaamat-as-Salaah), and give the Zakaah, and obey Allaah and His Messenger. Allaah will have His Mercy on them. Surely, Allaah is All-Mighty, All-Wise”
“Verily, Allaah will help those who help His (Cause). Truly, Allaah is All‑Strong, All‑Mighty.
41. Those (Muslim rulers) who, if We give them power in the land, (they) enjoin Iqamat‑as‑Salaah [i.e. to perform the five compulsory congregational Salaah (prayers) (the males in mosques)], to pay the Zakaah and they enjoin Al‑Ma‘roof (i.e. Islamic Monotheism and all that Islam orders one to do), and forbid Al‑Munkar (i.e. disbelief, polytheism and all that Islam has forbidden) [i.e. they make the Qur’aan as the law of their country in all the spheres of life]. And with Allaah rests the end of (all) matters (of creatures)”
“And whosoever fears Allaah and keeps his duty to Him, He will make a way for him to get out (from every difficulty).
3. And He will provide him from (sources) he never could imagine. And whosoever puts his trust in Allaah, then He will suffice him. Verily, Allaah will accomplish his purpose. Indeed Allaah has set a measure for all things”
And there are many verses with similar meanings. End quote.
Majmoo’ Fataawa Ibn Baaz (9/150-152).
And Allaah knows best.