All perfect praise be to Allah, the Lord of the worlds. I testify that there is none worthy of worship except Allah and that Muhammad, sallallaahu ‘alayhi wa sallam, is His slave and Messenger.
If the matter is as you described, that the grave is behind the masjid and thus outside of it, then prayer in it is valid. Your doubt as to whether the masjid was built to honor the shrine or not does not affect the validity of the prayer. What counts is that there is no grave inside the masjid.
Shaykh Al-Islam Ibn Taymiyyah said, "If the masjid is separate from the grave, then the ruling on it is the same as that on all other masjids." [Majmooʻ Al-Fataawa]
Allah knows best.