The site of the masjid had been a walled garden belonging to Banu Najjar, containing cultivated ground, date palms, and graves of the polytheists. The Messenger of Allah ﷺ said, "Name me a price for it."They replied, "We seek no price for it."So the palms were cut down, the cultivated ground was leveled, and the graves of the polytheists were dug up.Anas bin Malik reported that when the Messenger of Allah ﷺ arrived in Madinah, he stopped in the upper part of the city among a clan called Banu Amr bin Awf, and he stayed with them for fourteen nights. Then he sent for Banu Najjar, and they came wearing their swords.Anas said, "It is as though I can still see the Messenger of Allah ﷺ on his mount, with Abu Bakr riding behind him and the men of Banu Najjar all around him, until he set his belongings down in the courtyard of Abu Ayub."The Messenger of Allah ﷺ would pray wherever the time of prayer overtook him, even praying in the folds where sheep were kept. Then he gave orders for a masjid to be built, and he sent for Banu Najjar and said, "Banu Najjar, name me a price for this walled garden of yours."They said, "By Allah, we ask no price for it except from Allah ."Anas said, "What was in it was just as I am telling you. It contained graves of the polytheists, it contained ruins, and it contained date palms."The Messenger of Allah ﷺ ordered that the graves of the polytheists be dug up, that the ruins be leveled, and that the palms be cut down. The palm trunks were then set in a row as the qiblah of the masjid, and its doorposts were made of stone. They carried the rocks while chanting verses, and the Prophet ﷺ was with them, saying, "Allah, there is no good except the good of the Hereafter, so grant victory to the Ansar and the Muhajirah."In another wording he said "forgive" in place of "grant victory."Musa said that Abdul-Warith also narrated it to them in similar terms. Abdul-Warith used to say "ruins," and he claimed that he was the one who passed this hadith on to Hammad.
حدثنا موسى بن إسمعيل حدثنا حماد بن سلمة عن أبي التياح عن أنس بن مالك قال كان موضع المسجد حائطا لبني النجار فيه حرث ونخل وقبور المشركين فقال رسول الله ﷺ ثامنوني به فقالوا لا نبغي به ثمنا فقطع النخل وسوي الحرث ونبش قبور المشركين وساق الحديث وقال فاغفر مكان فانصر قال موسى وحدثنا عبد الوارث بنحوه وكان عبد الوارث يقول خرب وزعم عبد الوارث أنه أفاد حمادا هذا الحديث
[Its chain of transmission is sahih] Ibn Majah narrated it in the Masajid section, chapter "Where it is permissible to build mosques," hadith no. 742, from the hadith of Hammad bin Salama, with the same wording. See also the preceding hadith.