The correct view, however, is that the reasons for these prohibitions differ. Sometimes the reason is resemblance to the polytheists, as with prayer at graves; sometimes it is that these places are refuges for devils, as with camel enclosures; and sometimes it is something else. Allah knows best.
He was asked:
If a Muslim has no choice but to pray in a bathhouse and fears that the prayer time will expire, is it permissible for him to do so?
He answered:
If he cannot bathe, leave, and pray before the prayer time expires, he should bathe and pray in the bathhouse. Praying within the prescribed time in places where prayer is prohibited is preferable to praying elsewhere after the time has expired. Thus, if someone is confined in a latrine, he should pray there. There is disagreement over whether he must repeat the prayer, but the correct view is that he need not repeat it. Likewise, a person should pray unclothed within the prescribed time if he cannot do otherwise.
However, if he can bathe, leave the bathhouse, and pray outside it within the prescribed time, he may not pray in the bathhouse. Similarly, if he can bathe at home, he may not pray in the bathhouse except when there is a need. Allah knows best.