It is narrated from Sayyiduna Abu Hurayrah (may Allah be pleased with him) that when this verse was revealed: "Whoever does evil will be recompensed for it" [Surah al-Nisa: 123], it weighed very heavily on the Muslims. They mentioned it to the beloved Prophet (peace and blessings be upon him), and he (peace and blessings be upon him) said: "Keep to moderation, stay on course, and rejoice. Indeed, whatever hardship befalls a Muslim is an expiation for his sins, even a thorn that pricks him and a calamity that strikes him (is a cause of expiation for his sins)."
Hadith Referenceالسنن الكبرى للبيهقي / كتاب الجنائز / 6535