Abu Usaid al-Ansari reported the Messenger of Allah ﷺ as saying: when any of you enters the mosque he should invoke blessing on the prophet ﷺ and then he should say: O Allah, open to me the gates of thy mercy. And when he goes out, he should say: O Allah, I ask thee out of Thine abundance.
Hadith Referenceسنن ابي داود / كتاب الصلاة / 465
Hadith Gradingالألبانی:صحيح | زبیر علی زئی:إسناده صحيح، صحيح مسلم (713 ولم يذكر: فليسلم علي النبيﷺ)
´It was narrated that 'Abdul-Malik bin Sa'eed said:` "I heard Abu Humaid and Abu Usaid say: 'The Messenger of Allah (ﷺ) said: "When any one of you enters the Masjid, let him say: 'Allahumma aftahli abwaba rahmatik (O Allah, open to me the gates of your mercy). And when he leaves let him say: Allahumma inni as'aluka min fadlik (O Allah, I ask You of Your bounty).'"
Hafiz Muhammad Ameen
730. Commentary: When entering, the aim is to attain Allah’s mercy, and upon coming out, one engages in seeking sustenance; therefore, both supplications are appropriate to their respective occasions. By “mercy” is meant blessings of the Hereafter and forgiveness. “Fadl” (bounty) refers to both worldly blessings and sustenance.
Source: Sunan Nasa'i: Translation and Benefits by Shaykh Hafiz Muhammad Amin Hafizullah, Page: 730