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.
Dr. Muzammil H. Siddiqi, President of the Fiqh Council of North America, stated: 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.