The Messenger of Allah ﷺ said, "When one of you enters the mosque, let him say, 'O Allah, open for me the gates of Your mercy.' And when he leaves, let him say, 'O Allah, I ask You of Your bounty.'"
قال رسول الله ﷺ إذا دخل أحدكم المسجد فليقل: اللهم افتح لي أبواب رحمتك، وإذا خرج فليقل: اللهم إني أسألك من فضلك.
Muslim narrated it in "The Prayer of Travelers" (713), from Yahya bin Yahya, who said, Sulayman bin Bilal informed us, from Rabia bin Abu Abdul-Rahman, from Abdul-Malik bin Saeed, from Abu Humayd, or Abu Usayd, and he mentioned the same. Muslim said, I heard Yahya bin Yahya say, I wrote down this hadith from the book of Sulayman bin Bilal, who said, It has reached me that Yahya Himani says, "and Abu Usayd."Abu Dawud narrated it (465) through the hadith of Abdul-Aziz bin Muhammad Darawardi, from Rabia bin Abu Abdul-Rahman, with the same chain, and he added at the beginning of the hadith, "When one of you enters the masjid, let him send peace upon the Prophet ﷺ and then say," and he mentioned the same. This addition is sahih, and in some of the narrations he also sends peace upon leaving.