Sheikh Muhammad Muhammad Sâlim `Abd al-Wadûd
The prohibition of mentioning Allah's name in the place where people relieve themselves – whether it be an indoor bathroom or an open field – is essentially a prohibition of mentioning Allah's name in a place where impurity accumulates.
The prohibition, furthermore, is one regarding good manners and does not reach the level of sin. It is simply one of mannerly behavior towards Allah' name.
Modern bathrooms that use plumbing to remove waste after a person completes relieving himself are generally not places where impurity remains and accumulates, especially when these bathrooms are properly maintained. A Muslim can utter Allah’s name while in such a restroom without any objection as long as he is not actually in the process of relieving himself.
Therefore, if a Muslim sneezes or has some other occasion to say Allah’s name while being present in the restroom, and he is in a state other than that of relieving himself, then it is permissible for him to do so.
If the person sneezes while relieving himself, then he should say “al-Hamdu Lillâh” silently within himself without uttering it.
And Allah knows best.
Source: Islam Today