The Messenger of Allah ﷺ said: “One prayer in this mosque of mine is better than a thousand prayers offered anywhere else, except the Sacred Mosque; and a prayer in the Sacred Mosque is a hundred times better than a prayer in my mosque.”
Abu ʿAbd Allah al-Maqdisī noted that its chain of transmission meets the standards of authenticity.
For this reason, the Sacred Law prescribed the devotional retreat (iʿtikāf) in mosques, replacing the pre-Islamic custom of secluding oneself in the Cave of Ḥirāʾ. The Prophet ﷺ used to observe iʿtikāf during the last ten nights of Ramadan until Allah took him in death.
Iʿtikāf is a legislated act of worship to be performed in mosques by unanimous agreement of the leading scholars, as the Exalted says: “Do not approach your wives while you are in retreat in the mosques” (al-Baqarah 2:187)—meaning that while you are stationed in the mosques you must not have marital relations with them, even if the intercourse were to occur outside the mosque.
Accordingly, the jurists state that the essential pillar of iʿtikāf is remaining in the mosque for the worship of Allah, and its nullifier is sexual intercourse.
As for lingering in devotion beside a tree or a stone—whether fashioned into an idol or not—or keeping vigil at the grave of a prophet or anyone else, or at the station of a prophet or anyone else, none of this belongs to the religion of the Muslims. Rather, it resembles the religion of the polytheists, about whom Allah tells us in His Book, when He says: