Qays says: I heard Abdullah (may Allah be pleased with him) say: We used to go on military expeditions and we had no women with us. We said: "Should we not castrate ourselves?" So he (peace and blessings be upon him) forbade us from that. Then we were given permission to marry women for a garment. Then he recited this verse: "O you who believe, do not make unlawful the good things that Allah has made lawful for you, and do not transgress. Indeed, Allah does not love the transgressors." [Surah Al-Ma'idah: 87]
Hadith Referenceالسنن الكبرى للبيهقي / كتاب النكاح / 13464