The site of the Prophet's ﷺ mosque belonged to Banu Najjar. It contained palm trees and graves of polytheists. The Prophet ﷺ said to them, "Set me a price for it."They replied, "We will never accept any price for it."The Prophet ﷺ then began building it while they handed him the materials, and the Prophet ﷺ was saying, "Indeed, true life is the life of the Hereafter, so forgive the Ansar and the Muhajirun."He said, before the mosque was built, the Prophet ﷺ used to pray wherever the time of prayer overtook him.
حدثنا علي بن محمد حدثنا وكيع عن حماد بن سلمة عن أبي التياح الضبعي
كان موضع مسجد النبي ﷺ لبني النجار وكان فيه نخل ومقابر للمشركين فقال لهم النبي ﷺ ثامنوني به قالوا لا نأخذ له ثمنا أبدا قال فكان النبي ﷺ يبنيه وهم يناولونه والنبي ﷺ يقول ألا إن العيش عيش الآخره فاغفر للأنصار والمهاجره قال وكان النبي ﷺ يصلي قبل أن يبني المسجد حيث أدركته الصلاة
Bukhari narrated it in "The Prayer," in the chapter "Are the graves of the polytheists of the pre-Islamic era dug up..." etc., hadith no. 428 and 1868 and elsewhere, and Muslim narrated it in "Masjids," in the chapter "The building of the Prophet's masjid," hadith no. 524, from the hadith of Abu Tayah, with this wording.