On the day of Badr, the Prophet ﷺ accepted ransom from the captives, and Allah revealed, "It is not fitting for a prophet that he should have prisoners of war until he has thoroughly subdued the land," up to His saying, "a severe punishment would have touched you for what you took" [Anfal 8:67–68], referring to the ransom. Then Allah made the spoils of war lawful for them.
حدثنا أحمد بن محمد بن حنبل قال حدثنا أبو نوح قال أخبرنا عكرمة بن عمار قال حدثنا سماك الحنفي قال حدثني ابن عباس قال
لما كان يوم بدر فأخذ يعني النبي ﷺ الفداء أنزل الله عز وجل {ما كان لنبي أن يكون له أسرى حتى يثخن في الأرض} إلى قوله {لمسكم فيما أخذتم} من الفداء ثم أحل لهم الله الغنائم
Muslim narrated it in the book of jihad and expeditions, chapter on reinforcement by angels at the Battle of Badr and the permissibility of spoils of war, hadith no. 1763, from the hadith of Ikrima bin Ammar with this same chain. It is also found in Musnad Ahmad, 1/30, 33.