If the rooms attached to the mosque were built to be part of the mosque, i.e., the builder of the mosque intended that they should be part of the mosque in which prayers are offered, then they come under the same rulings as the mosque, so the two rak’ahs for greeting the mosque should be offered when entering them, and women who are menstruating or bleeding following childbirth should not be allowed to stay in them.
But if these room are intended to be extra rooms for teaching or holding meetings, or a place where the imam or muezzin lives, and are not intended to be a place for prayers, then in that case they do not come under the same rulings as the mosque.
If the intention of the one who built the mosque is not known, then the basic principle is that whatever is within the walls of the mosque and has a door opening into the mosque comes under the same rulings as the mosque.
This is what may be understood from the words of the scholars concerning the rooms and other facilities that are found in the mosques, taking into account the differences in the situation nowadays as compared with what was prevalent at the time of the earlier fuqaha’.
There follow some fatwas of the scholars concerning that:
Shaykh Ibn Baaz (may Allah have mercy on him) was asked:
In America there is a mosque that consists of three floors. The highest floor is a prayer room for women, the floor beneath that is the main prayer space, and the floor beneath that -- the basement -- contains washrooms, a place for Islamic magazines and newspapers, classrooms for women and an additional space for women to pray. Is it permissible for women who are menstruating to enter this lower level?
He replied: If the building mentioned was prepared as a mosque and the people on the upper and lower floors can hear the voice of the imam, the prayers of all people in the mosque are valid and it is not permissible for menstruating women to sit in the place that has been prepared for prayers on the lowest floor, because it is part of the mosque.
But if the lower floor was not intended by the sponsor to be part of the mosque; rather he intended it to be a storeroom or a place for the things mentioned in the question, then it does not come under the same rulings as the mosque and it is permissible for women who are menstruating or people who are in a state of impurity (janaabah) to sit there, and there is nothing wrong with praying there in clean places that are not part of the washrooms, like any other clean place in which there is no shar’i impediment to prayer. But the one who prays there should not follow the imam who is upstairs if he cannot see him or see some of the people who are praying behind him, because it is not part of the mosque according to the more sound of the two scholarly opinions. End quote from Fataawa al-Shaykh Ibn Baaz, 10/221
Shaykh Ibn ‘Uthaymeen (may Allah have mercy on him) was asked:
Is it permissible to observe i’tikaaf in the rooms inside the mosque?
He replied: That depends. The one who looks at the general meaning of the words of the fuqaha’ will say that it is part of the mosque, because the room that is enclosed within the wall of the mosque is regarded as part of the mosque. But for the one who thinks that it was not built to be part of the mosque and that it is a room for the imam, it is like the apartments of the Messenger (blessings and peace of Allah be upon him). The doors of the apartments of the Messenger (blessings and peace of Allah be upon him) opened on to the mosque but they were (separate) houses, and the Messenger (blessings and peace of Allah be upon him) did not go out to them when he was in i’tikaaf. So to be on the safe side we say that the one who is observing i’tikaaf should not do so in such rooms. But the people nowadays usually regard rooms in the mosque as part of the mosque. End quote from Sharh al-Kaafi.
He (may Allah have mercy on him) was also asked: What is the ruling on praying tahiyyat al-masjid for one who enters the library of the mosque in the following cases:
(i)When the door of the library is inside the mosque
(ii)when the door of the library is outside the mosque
(iii)when the library has two doors, one inside and one outside?
In the first case, which is when the door of the library is inside the mosque, the library is part of the mosque so it comes under the same rulings, and it is prescribed for the one who enters it to pray tahiyyat al-masjid, and it is not permissible for one who is junub to stay there unless he has wudoo’; it is valid to observe i’tikaaf there, and it is haraam to buy and sell there, and all the other well-known rulings on the mosque apply to it.
In the second case, which is when the door is outside the mosque and there is no door from the library into the mosque, it is not part of the mosque and the rulings on mosques do not apply to it. So tahiyyat al-masjid should not be offered when entering it, i’tikaaf is not valid there, it is not haraam to buy and sell there, because it is not part of the mosque as it is separate from it.
In the third case, which is when the library has two doors, one of which is inside the mosque and the other is outside: if the wall of the mosque encompasses it then it is part of the mosque and the rulings on mosques apply to it. If it is not encompassed by it; rather it has its own separate wall, then it does not come under the same rulings as the mosque, because it is separate from the mosque. Hence the houses of the Prophet (blessings and peace of Allah be upon him) were not part of his mosque, even though they had doors opening into the mosque, because they were separate from it.
End quote from Fataawa al-Shaykh Ibn ‘Uthaymeen, 14/351.
And Allah knows best.