Sayyiduna Ubadah bin al-Samit (may Allah be pleased with him) said: The Messenger of Allah (peace and blessings be upon him) set out on the day of Badr and met the enemy. When Allah defeated them, a group of the Muslims pursued them, killing them; a group surrounded the Messenger of Allah (peace and blessings be upon him); and a group set about gathering the wealth. When Allah Almighty had sufficed against the enemy and those who had killed them returned, they said: We killed the enemy, so the spoils are ours, and it was because of us that Allah defeated them. Those who had surrounded the Messenger of Allah (peace and blessings be upon him) said: By Allah, you are not more deserving than us, because we surrounded the Messenger of Allah (peace and blessings be upon him) so that the enemy could not reach him. And those who had gone to the wealth said: We are the ones who deserve it. So Allah Almighty revealed the verse: "They ask you about the spoils of war" [Surah al-Anfal: 1]. Then the Messenger of Allah (peace and blessings be upon him) divided it among them in a fitting manner.
Hadith Referenceالسنن الكبرى للبيهقي / كتاب قسم الفيء والغنيمة / 12715