Sayyiduna Ubadah bin al-Samit (may Allah be pleased with him) said: the Messenger of Allah (peace and blessings be upon him) set out towards Badr and met the enemy. When Allah defeated the enemy, a contingent of the Muslims pursued the enemy, killing them; another contingent surrounded the Messenger of Allah (peace and blessings be upon him), and another contingent took possession of the spoils and the camp. When those who had pursued the enemy returned, they said: "The spoils are ours, because we pursued the enemy, and through us Allah drove them away and defeated them." Those who had surrounded him (peace and blessings be upon him) said: "You are not more entitled to it than us. We guarded the Messenger of Allah (peace and blessings be upon him) so that the enemy would not harm him." And those who had taken possession of the spoils and the camp said: "You are not more entitled to it than us. We took possession of it and secured it." So Allah Almighty revealed this verse to the Messenger of Allah (peace and blessings be upon him): "They ask you about the spoils of war. Say: The spoils of war belong to Allah and the Messenger." [Surah al-Anfal: 1] A short while later, the Messenger of Allah (peace and blessings be upon him) divided the spoils among them equally.
Hadith Referenceالسنن الكبرى للبيهقي / كتاب السير / 17985