I asked, "Messenger of Allah, what do you say about things I used to do in the days of ignorance as acts of devotion, such as giving charity, freeing slaves, and maintaining ties of kinship? Is there any reward for me in them?"The Prophet ﷺ replied, "You have embraced Islam along with all the good you did before."
قلت: يا رسول الله أرأيت أشياء كنت أتحنث بها في الجاهلية، من صدقة أو عتاقة وصلة رحم، فهل فيها من أجر، فقال النبي ﷺ: أسلمت على ما سلف من خير.
Bukhari narrated it in the chapter on zakat (1436), and Muslim in the chapter on faith (123), both through the route of Mamar, from Zuhri, from Urwa, from Hakeem bin Hizam, who said, then he mentioned it. The wording given is that of Bukhari, and Muslim's wording is similar.