Question and Answer Details
In the Name of Allah, Most Gracious, Most Merciful.
All praise and thanks are due to Allah, and peace and blessings be upon His Messenger.
Dear brother in Islam, thanks for your interesting question. May Allah Almighty guide us all to the straight path of Islam.
It should be clear that doing good, helping others in distress, observing the religious duties, etc., bring about Allah's mercy and help. So, it is one of Allah's favors and blessings that He helps good people when they are in need or distress.
In his response to your question, Dr. Muzammil H. Siddiqi, President of the Fiqh Council of North America, stated,
Yes, indeed, good deeds, especially the deeds of charity, are very good in removing calamities and difficulties.
Allah has promised in the Qur'an that He will bless those who have faith and do righteous deeds both in this life and in the life to come.
Likewise, Prophet Muhammad (peace and blessings be upon him) described three people who took shelter in a cave at night and in the morning they saw a big stone that blocked the mouth of the cave. They were in great distress and feared that they would die inside the cave. They then prayed to Allah and each person remembered some good deeds that he did. They prayed to Allah to bless them, because they did those deeds only for His sake. The hadith says that at every mention of a good deed, they saw that the stone was moving a bit until it finally moved enough for them to get out of the cave.
This is a beautiful illustration of the efficacy of good actions in bringing the mercy of Allah.
Taken from here