Narrated 'Abdullah: We were with the Prophet while we were young and had no wealth whatever. So Allah's Apostle said, "O young people! Whoever among you can marry, should marry, because it helps him lower his gaze and guard his modesty (i.e. his private parts from committing illegal sexual intercourse etc.), and whoever is not able to marry, should fast, as fasting diminishes his sexual power."
— Sahih Bukhari, Volume 7, Book 62: Wedlock, Marriage (Nikaah), Hadith #4
Narrated 'Abdullah: We were with the Prophet while we were young and had no wealth whatever. So Allah's Apostle said, "O young people! Whoever among you can marry, should marry, because it helps him lower his gaze and guard his modesty (i.e. his private parts from committing illegal sexual intercourse etc.), and whoever is not able to marry, should fast, as fasting diminishes his sexual power."
This hadith is from the book of Wedlock, Marriage (Nikaah) in Sahih Bukhari, compiled by Imam al-Bukhari. It is hadith number 4 in book 62 of volume 7.
This hadith is referenced in the following topics: Virtues of Fasting, Eligibility of Marriage, Modesty, Extramarital Relations.