All perfect praise be to Allah The Lord of the Worlds and may His peace and blessings be upon our Prophet Mohammad and upon all his family and companions.
Not praying near a grave isn`t one of the conditions for a prayer .i.e. avail and valid. Therefore, praying near a grave is valid and availing without any reprehensibility. Although it is permissible, but becomes reprehensibile, if the praying person faced the grave without having a Sutrah {an object used by a person performing salat/prayer as a barrier between himself and one passing in front of him} while performing a prayer. Al-Imam An-Nawawi (May Allah have mercy on him) said: "Imam Shafie and the fellow scholars said that praying facing a grave is reprehensible, whether its dweller was righteous or not." {Al-Majmou`, vol.5/pp.316}.
Usually, the shrines of the Prophets and the companions are separated from the place of prayer; rather the graves themselves are separated from one another inside chambers. As a result, these mosques aren`t set on nor between graves; rather they are next to them, so it is permissible to pray in them. Moreover, even if they were built on graves, although reprehensible, this doesn`t mean that it is forbidden to pray there or that the prayer is invalid. Actually, none of the four Imams has ever ruled that it is forbidden to pray in mosques built on graves, because the validity of the prayer itself is a ruling that has nothing to do with the Mosque being built on a grave or next to one.
Al-Imam Al-Mouzni, Shafie jurist, said: "If he prayed on a grave or next to it, then it avails him." {Mokhtasar Al-Mouzni, vol.8/pp.112}.
Imam Shaibani, a Hanafi jurist, was once asked: "Do you find it reprehensible for a Mosque`s Qibla to face a toilet, an exit or a grave? He replied: "Yes." What if somebody offered prayers in it, does it avail them? He replied: "Yes, it does," [Al-Asil Al Ma`roof BilMabsoot, vol.1/pp.208].
In conclusion, it is permissible to pray in mosques that have shrines; particularly since the graves are outside the place of prayer and this isn`t reprehensible. And Allah Knows Best.