If it becomes obligatory for a muʿtakif to perform ghusl, in case the ghusl is one of the ghusls that is not permitted to be performed in a mosque – such as the ghusl for janābah, which would require staying in the mosque while in the state of janābah or would cause the mosque to become impure – then he must leave; otherwise, his iʿtikāf becomes invalid.
In case there is no problem in performing ghusl in the mosque – such as the ghusl for touching a corpse (mass al‑mayyit) – and it is possible to perform ghusl, then based on obligatory precaution, it is not permitted to leave the mosque.