Anas bin Malik reported,

The Prophet ﷺ arrived in Medina and stayed in the upper part of the city among a clan called Banu Amr bin Awf. The Prophet ﷺ remained with them for fourteen nights, then sent for the Banu Najjar, who came wearing their swords. It is as if I can still see the Prophet ﷺ on his mount, with Abu Bakr riding behind him and a crowd of Banu Najjar gathered around him, until he dismounted in the courtyard of Abu Ayub.He used to love praying wherever the time of prayer overtook him, and he would pray in sheep pens. Then he ordered that the mosque be built. He sent for a group of the Banu Najjar and said, "Banu Najjar, name me a price for this orchard of yours."They replied, "No, by Allah, we seek its price from no one but Allah."Anas said, In it was what I am about to tell you. There were graves of polytheists, ruins, and date palms. The Prophet ﷺ ordered that the graves of the polytheists be exhumed, the ruins leveled, and the palms cut down. They lined up the palm trunks as the qiblah of the mosque and made its doorposts of stone.They began carrying the rocks while chanting verses, and the Prophet ﷺ was with them, saying, "O Allah, there is no good except the good of the Hereafter, so forgive the Ansar and the Muhajirun."

Bukhari 428

Sahih

حدثنا مسدد قال حدثنا عبد الوارث عن أبي التياح

عن أنس بن مالك قال

قدم النبي ﷺ المدينة فنزل أعلى المدينة في حي يقال لهم بنو عمرو بن عوف فأقام النبي ﷺ فيهم أربع عشرة ليلة ثم أرسل إلى بني النجار فجاءوا متقلدي السيوف كأني أنظر إلى النبي ﷺ على راحلته وأبو بكر ردفه وملأ بني النجار حوله حتى ألقى بفناء أبي أيوب وكان يحب أن يصلي حيث أدركته الصلاة ويصلي في مرابض الغنم وأنه أمر ببناء المسجد فأرسل إلى ملإ من بني النجار فقال يا بني النجار ثامنوني بحائطكم هذا قالوا لا والله لا نطلب ثمنه إلا إلى الله فقال أنس فكان فيه ما أقول لكم قبور المشركين وفيه خرب وفيه نخل فأمر النبي ﷺ بقبور المشركين فنبشت ثم بالخرب فسويت وبالنخل فقطع فصفوا النخل قبلة المسجد وجعلوا عضادتيه الحجارة وجعلوا ينقلون الصخر وهم يرتجزون والنبي ﷺ معهم وهو يقول اللهم لا خير إلا خير الآخره فاغفر للأنصار والمهاجره

البخاري 428صحيح
Contents
Reference
Commentary
Ayat
Collection
Sahih Bukhari
Book
Prayer
Chapter
Should the graves of the polytheists of Jahiliyyah be exhumed and mosques built in their place
Numbering
Darussalam 428
Bukhari 428