Muhammad sometimes performed the prayers with his shoes on. The base of the mosque was not covered with carpets at that time, but it was of stone and soil. So, there is no need to take the shoes off while performing the prayers on soil ground. If shoes have any dirt on them, it should be removed before the prayer. Following narrations are evidence for this:
Narrated from Abu Maslamah: I asked Anas ibn Malik “Did the messenger perform any prayers while his shoes on?” Anas said “Yes”. (Bukhari, Prayers, 24; Tirmidhi, Prayers, 181)
Tirmidhi wrote down after this narration: “Scholars practice with this hadith”.
Abdullah ibn Masud said: We have certainly seen the messenger performing prayers with his shoes on. (Abu Dawud, Prayers, 88; Ibn Majah, Performing Prayers, 66)
Abu Said al-Hudri said: The messenger took off his shoes and put them aside before he performed a prayer with his friends. The ones who saw this also took off their shoes. When the messenger finished the prayer, he asked “Why did you take off your shoes?” They said “We took them off because you did so.” The messenger said “Gabriel informed me that there was dirt on my shoes.” He continued “When you come into a mosque, check your shoes and clean it if there is any dirt. Then perform the prayers with your shoes on.” (Abu Dawud, Prayers, 88)
Shaddad ibn Aws narrated from his father: The messenger said “Make opposition to the Jews. They do not perform the prayers with their shoes on. You do so.” (Abu Dawud, Prayers, 88)
The statements like “You do so” or “Perform the prayers with shoes on” do not stand for an order, but freeness. Scholars stated that one can perform the prayers with shoes on as long as they are clean. One can also perform the prayers barefoot. It is optional. It can be also understood from a lot of other narrations in which the messenger prays either barefoot or with his shoes on. (See: Abu Dawud, Prayers, 88)