There is evidence in the Sunnah to indicate that if a person did a righteous deed whilst he was a kaafir, then he repented and became Muslim, there will be recorded for him after his repentance the reward of the righteous deeds that he did before, as if he did them in Islam. This is the great generosity of Allaah and His immense bounty and kindness. Although some scholars disagreed with that, this is the correct view and it is the apparent meaning of the saheeh Sunnah.
It was narrated from Hakeem ibn Hizaam (may Allaah be pleased with him) that he said: I said: O Messenger of Allaah, what do you think of things that I did as acts of worship during the Jaahiliyyah such as giving charity, freeing slaves and upholding the ties of kinship – is there any reward for them? The Messenger of Allaah (blessings and peace of Allaah be upon him) said: “You have become Muslim with all your previous virtues.”
Narrated by al-Bukhaari (1436) and Muslim (123).
Al-Haafiz ibn Rajab al-Hanbali (may Allaah have mercy on him) said, commenting on this hadeeth:
If a person did a righteous deed, then he did a bad deed which cancelled it out, then he repented, then the reward that was lost because of his bad deed will be restored to him. End quote.
Fath al-Baari by Ibn Rajab (1/146)
Shaykh al-Islam Ibn Taymiyah (may Allaah be pleased with him) said:
If the hypocrite or show-off repents, does he have to repeat (prayers etc), or will his repentance have the effect of reversal on the deeds he did before that, so that he will be rewarded for them, or is it that he does not have to repeat anything and will not be rewarded?
As for repeating, the hypocrite does not have to do it at all, because a group of hypocrites repented from hypocrisy at the time of the Messenger of Allaah and he did not order any of them to repeat. And Allaah says (interpretation of the meaning):
“and they could not find any cause to do so except that Allaah and His Messenger had enriched them of His Bounty. If then they repent, it will be better for them, but if they turn away; Allaah will punish them with a painful torment in this worldly life and in the Hereafter”
Moreover, the hypocrite is inwardly a kaafir, so when he believes he is forgiven what he did before, so he does not have to make anything up, just as that is not required of the one who was outwardly a kaafir when he becomes Muslim.
With regard to his reward for what he did before, when he repents he is like the kaafir if he did righteous deeds when he was a kaafir then became Muslim -- will he be rewarded for it? In al-Saheehayn it is narrated that the Prophet (blessings and peace of Allaah be upon him) said to Hakeem ibn Hizaam: “You have become Muslim with all your previous virtues.”
With regard to the show-off, if he repents from showing off, even though he believed it was obligatory, then it is similar to the issue of what we are speaking of here, which is the issue of one who did not carry out an obligatory duty, even if he was not inwardly a kaafir; obliging him to make ups or repeat things is a major deterrent to repentance. End quote.
Majmoo’ al-Fataawa (22/20-21)
He also said (may Allaah have mercy on him):
The one who repents from sin is like the one who did not sin, and if the sin is erased then the punishment and consequences are also erased. Negation of good deeds is one of the consequences of sin. End quote.
Sharh al-‘Umdah (1/39).
Ibn Qayyim al-Jawziyyah (may Allaah have mercy on him) said:
Chapter: if his recent bad deeds supersede his past good deeds and cancel them out, then he repents sincerely from them, his good deeds are restored to him and he does not come under the same ruling as one who starts all over again, rather it is said to him: You have repented on the basis that what you did previously of good deeds (will be kept for you). The good deeds that he did in Islam are greater than the good deeds that the kaafir did when he was a kaafir, such as freeing slaves, giving charity and upholding the ties of kinship. Hakeem ibn Hizaam said: O Messenger of Allaah, what do you think of things that I did as acts of worship during the Jaahiliyyah such as giving charity, freeing slaves and upholding the ties of kinship – is there any reward for them? The Messenger of Allaah (blessings and peace of Allaah be upon him) said: “You have become Muslim with all your previous virtues.”
That is because sins that come between two acts of obedience are erased by means of repentance, so it becomes as if they never happened, and the two acts of obedience come together and will be counted together. And Allaah knows best. End quote.
Madaarij al-Saalikeen (1/282)
And Allaah knows best.