It is narrated from Sayyiduna Abdullah bin Abbas (may Allah be pleased with them both) regarding "O you who believe, fasting has been prescribed for you as it was prescribed for those before you, so that you may become God-fearing": In the time of the Prophet (peace and blessings be upon him), the people's situation was that when they had prayed the Isha prayer, food and drink became forbidden to them, and they would fast until the following night. Then a man betrayed himself: after praying Isha, without having broken his fast, he had intercourse with his wife. So Allah Almighty willed to make that a means of ease, mercy and benefit for the rest of the people, and He said: "Allah knew that you used to betray yourselves, so He turned to you in mercy" up to where He said: "Then complete the fast until the night." So this was something by which Allah Almighty benefited the people.
Imam Abu Dawud (may Allah have mercy on him) also narrated it with a similar meaning, through Ahmad bin Muhammad bin Shabbuyah, from Ali bin al-Husayn.