When the Prophet (may Allah's peace and blessings be upon him) entered the mosque, he would say: "a‘ūdhu billāhi al-‘azhīm wa biwajhihi al-karīm wa sultānihi al-qadīm min ash-shaytān ar-rajīm" (I seek refuge in Allah, the Majestic, and in His noble Countenance, and in His eternal dominion, from the accursed devil)
When any of you enters the mosque, let him say: 'Allahumma iftah li abwāba rahmatik' (O Allah, open for 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)