They both heard the Messenger of Allah ﷺ say, "No fatigue, illness, anxiety, sorrow, harm, or distress befalls a Muslim, not even a thorn that pricks him, except that Allah expiates some of his sins by it."
أنهما سمعا رسول الله ﷺ يقول: ما يصيب المسلم من نصب ولا وصب، ولا هم ولا حزن، ولا أذى ولا غم، حتى الشوكة يشاكها إلا كفر الله بها من خطاياه.
Bukhari related it in "The Sick" (5641–5642), and Muslim in "Righteousness, Kinship, and Manners" (2573), both of them through Muhammad bin Amr bin Ata, from Ata bin Yasar, from Abu Saeed Khudri and Abu Hurairah, who both related it, and the wording given is Bukhari's, while Muslim's wording is similar.