Abu Dharr reported,

I asked, "Messenger of Allah, which mosque was first established on earth?"He replied, "The Sacred Mosque."I asked, "Then which?"He replied, "The Aqsa Mosque."I asked, "How long was the period between them?"He answered, "Forty years. And wherever the time of prayer overtakes you, pray, for it is a mosque."In the version of Abu Kamil, "Then wherever the time of prayer overtakes you, pray it, for it is a mosque."

Muslim 1161

Sahih

حدثني أبو كامل الجحدري حدثنا عبد الواحد حدثنا الأعمش قال ح و حدثنا أبو بكر بن أبي شيبة وأبو كريب قالا حدثنا أبو معاوية عن الأعمش عن إبراهيم التيمي عن أبيه

عن أبي ذر قال

قلت يا رسول الله أي مسجد وضع في الأرض أول قال المسجد الحرام قلت ثم أي قال المسجد الأقصى قلت كم بينهما قال أربعون سنة وأينما أدركتك الصلاة فصل فهو مسجد وفي حديث أبي كامل ثم حيثما أدركتك الصلاة فصله فإنه مسجد

مسلم 1161صحيح
Contents
Reference
Commentary
Ayat
Collection
Sahih Muslim
Book
Mosques and places of prayer
Chapter
Mosques and places of prayer
Numbering
Darussalam 1161
Muslim 1161