Prophet Muhammad (peace be upon him) said: “No Muslim is afflicted with any harm but for that Allah will cause his sins to be shed as the leaves of a tree fall down.” [Sahih Bukhari] 
According to Islamic faith, Allah in His wisdom prescribed suffering for essential purposes. For the unjust, suffering is a punishment. This is a fact that has long been proved in scriptures when the fate of the people of Lut, the Pharaoh and his army, the people of Nuh, etc, is told. As for those who believe, even if they are sinners, suffering carries a different message. It could be a test through which Allah (subhana wa ta’ala) wants to elevate His pious servants to higher ranks in the hereafter. It could be a means to wash away the believer’s sins and bring him or her back to his or her earlier purity. 
Source: http://dailyhadith.adaptivesolutionsinc.com/hadith/Leaves-from-an-Autumn-Tree.htm