Muhammad himself was involved in the construction of the mosque. At the time, the mosque’s land belonged to two young orphans, Sahl and Suhayl, and when they learned that Muhammad wished to acquire their land to erect a mosque, they went to Muhammad and offered the land to him as a gift; Muhammad insisted on paying a price for the land because they were orphaned children. The price agreed upon was paid by Abu Ayyub al-Ansari, who thus became the endower or donor (wāqif) of the mosque, on behalf or in favor of Muhammad. al-Ansari also accommodated Muhammad upon his arrival at Medina in 622.
Originally an open-air building, the mosque served as a community center, a court of law, and a religious school. It contained a raised platform or pulpit (minbar) for the people who taught the Quran and for Muhammad to give the Friday sermon (khutbah). Subsequent Islamic rulers greatly expanded and decorated the mosque, naming its walls, doors and minarets after themselves and their forefathers. After an expansion during the reign of the Umayyadcaliphal-Walid I (reigned 705–715), it now incorporates the final resting place of Muhammad and the first two Rashidun caliphsAbu Bakr (reigned 632–634) and Umar (reigned 634–644). One of the most notable features of the site is the Green Dome in the south-east corner of the mosque, originally Aisha‘s house, where the tomb of Muhammad is located. Many pilgrims who perform the Hajj also go to Medina to visit the Green Dome.
In 1909, under the reign of OttomanSultanAbdul Hamid II, it became the first place in the Arabian Peninsula to be provided with electrical lights. From the 14th century, the mosque was guarded by eunuchs, the last remaining guardians were photographed at the request of then-PrinceFaisal bin Salman Al Saud, and in 2015, only five were left. It is generally open regardless of date or time, and has only been closed to visitors once in modern times, as Ramadan approached during the COVID-19 pandemic in 2020.