I asked the Messenger of Allah ﷺ, "Which mosque was first placed on the earth?"He replied, "The Sacred Mosque."I asked, "Then which?"He said, "Then Masjid al-Aqsa."I asked, "How long was there between them?"He answered, "Forty years."I asked, "Then which?"He said, "Then wherever prayer overtakes you, pray, for all of it is a mosque."
حدثنا سفيان عن الأعمش عن إبراهيم التيمي عن أبيه
سألت رسول الله ﷺ أي مسجد وضع في الأرض أول قال المسجد الحرام قلت ثم أي قال ثم المسجد الأقصى قلت كم بينهما قال أربعون سنة قلت ثم أي قال ثم حيثما أدركت الصلاة فصل فكلها مسجد
Its isnad is sahih. Ibrahim Taymi is Ibn Yazid bin Sharik, and it is in Muslim 1/370 no. 520, in the chapter on mosques, and Ibn Majah 1/247 no. 753, and Ibn Khuzayma 2/5 no. 787.