And those who, when they commit an immorality or wrong themselves [by transgression], remember Allah and seek forgiveness for their sins - and who can forgive sins except Allah? - and [who] do not persist in what they have done while they know.
وَالَّذِينَ إِذَا فَعَلُوا فَاحِشَةً أَوْ ظَلَمُوا أَنفُسَهُمْ ذَكَرُوا اللَّهَ فَاسْتَغْفَرُوا لِذُنُوبِهِمْ وَمَن يَغْفِرُ الذُّنُوبَ إِلَّا اللَّهُ وَلَمْ يُصِرُّوا عَلَىٰ مَا فَعَلُوا وَهُمْ يَعْلَمُونَ
The Messenger of Allaah (sallallaahu alayhi wa sallam) said:
There is no slave who commits a sin then performs ablution and makes excellent [his ablution] and then prays two Rak'ah and then seeks forgiveness from Allaah from his sin except that Allaah forgives him.
ما مِن عبدٍ يُذنِبُ ذنبًا ثمَّ يتوضَّأُ ثمَّ يُصَلِّي ركعتَيْنِ ثمَّ يستغفِرُ اللهَ لذلك الذَّنبِ إلَّا غفَر اللهُ له
[Sahih Ibn Hibbaan]
Ibn Baaz said the Hadiith is established
Sheikh al-Albaani said the Hadiith is Sahih
Ahmad Shaakir said the Hadiith has a Sahih chain of narrators
Ibn Hibbaan included the Hadiith in his collection of Sahih Hadiith
al-Munziri said its chain of narrators is Sahih or Hasan or near to it
Abu Huraira reported Allah's Messenger (sallallaahu alayhi wa sallam) having said:
By Him in Whose Hand is my life, if you were not to commit sin, Allah would sweep you out of existence and He would replace (you by) those people who would commit sin and seek forgiveness from Allah, and He would have pardoned them.
[Sahih Muslim Hadith No. 2749]
The occurrence of sin is inevitable. Allaah decreed this. As a result of this we are able to see the signs of His forgiveness and mercy. Had there been no sins then how could you see the signs of Allaah's mercy and His forgiveness.
Allaah decreed that humans and jinn would sin and then He would forgive those who repented. He would forgive those He wills to forgive.
The Hadith is not an exhortation to sin. Allaah has forbidden us to sin and told us to stay away from all impermissible things.
However, He had decreed and He knew that sins would occur. The Hadiith reminds us that there is no escaping sins, and that the believer should not despair of the mercy of Allaah.
Every time you make a mistake and sin, turn to Allaah and repent. He will forgive you.
He is al-Tawwaab, al-Rahiim, al-Afww, al-ghafuur.