Abu Huraira reported: The Messenger of Allah, peace and blessings be upon him, said, “Whoever enters our mosque in order to teach goodness, or to learn it himself, he is striving in jihad in the way of Allah. Whoever enters it for another reason, he is viewing what does not belong to him.”
Source: Musnad Aḥmad 8396
Grade: Hasan (fair) according to Ahmad Shakir
عَنْ أَبِي هُرَيْرَةَ عَنْ رَسُولِ اللَّهِ صَلَّى اللَّهُ عَلَيْهِ وَسَلَّمَ قَالَ مَنْ دَخَلَ مَسْجِدَنَا هَذَا لِيَتَعَلَّمَ خَيْرًا أَوْ لِيُعَلِّمَهُ كَانَ كَالْمُجَاهِدِ فِي سَبِيلِ اللَّهِ وَمَنْ دَخَلَهُ لِغَيْرِ ذَلِكَ كَانَ كَالنَّاظِرِ إِلَى مَا لَيْسَ لَهُ
8396 مسند أحمد باقي مسند المكثرين
16/248 المحدث أحمد شاكر خلاصة حكم المحدث إسناده حسن في مسند أحمد