Sayyiduna Abdullah bin Abbas (may Allah be pleased with them both) narrates: One of the Companions of the Prophet (peace and blessings be upon him) had a wife. He asked the Prophet (peace and blessings be upon him) for permission to go out on some errand, and the Prophet (peace and blessings be upon him) gave him permission. He went out on a rainy day, and there was a woman bathing by a well. When he came as close to the woman as a man is to a woman during intercourse and began to move his private part, it did not become erect. He got up, came to the Prophet (peace and blessings be upon him) and mentioned this to him. The Prophet (peace and blessings be upon him) said to him: "Pray four rak'ahs." And Allah Almighty revealed this verse about it: "Establish prayer at the two ends of the day and in a part of the night. Indeed, good deeds wipe away evil deeds."
Imam al-Bazzar recorded it, and its narrators are the narrators of the Sahih.
Hadith Referenceمجمع الزوائد ومنبع الفوائد / كتاب التفسير / 11081