Abu Usaid reported that the Messenger of Allah (ﷺ) said: When any one of you enters the mosque, he should say: "O Allah! open for me the doors of Thy Mercy" ; and when he steps out he should say: 'O Allah! I beg of Thee Thy Grace." (Imam Muslim said: I heard Yahya saying: I transcribed this hadith from the compilation of Sulaiman bin Bilal (RA) ).
Hadith Referenceصحيح مسلم / كتاب صلاة المسافرين وقصرها / 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