Narrated Abu Usaid reported God's Messenger as saying:
When any of you enters the mosque he should say, “O God, open to me the gates of Thy mercy, “and when he goes out he should say “O God, I ask Thee out of Thine abundance.”
Ka'b b. Malik said that the Prophet always came back from a journey by day in the forenoon, and when he arrived he went first to the mosque, and having prayed two rak'as in it he sat down in it.
He also reported God’s Messenger as saying, “A man's prayer in company is twenty-five times as valuable as his prayer in his house and his market, for when he performs ablution, doing it well, then goes out to the mosque, having no other reason than prayer for going out, he does not take a step without being raised a degree for it and having a sin remitted for it, and when he prays the angels continue to invoke blessings on him as long as he is in his place of prayer, saying, ‘God bless him; God show mercy to him.’ And each of you continues to be engaged in prayer as long as he is waiting for the prayer.” In a version he said, “When he enters the mosque prayer holds him fast.” And he added in the invocation of the angels, “O God, forgive him, O God, turn towards him, as long as he does not do any harm in it and as long as he does not do anything unseemly in it.”