This hadeeth was narrated by Ahmad (12173) from Anas ibn Maalik from the Prophet (peace and blessings of Allaah be upon him), who is reported to have said, “Whoever prays forty prayers in my Mosque, missing no prayer, it will be recorded that he is safe from the Fire, is saved from punishment and is free of hypocrisy.” This is a da’eef (weak) hadeeth.
This was mentioned by Shaykh al-Albaani in al-Silsilah al-Da’eefah (364), who said: It is da’eef. He also mentioned it in Da’eef al-Targheeb (755) and said, it is munkar (a kind of weak hadeeth).
Al-Albaani said in his book Hujjat al-Nabi (peace and blessings of Allaah be upon him) (p. 185) that it is an innovation (bid’ah) to visit Madeenah and tell the visitors to Madeenah to stay there for a week so that they will be able to offer forty prayers in the Prophet’s Mosque so that they will be free from hypocrisy and saved from the Fire.”
Shaykh Ibn Baaz said:
With regard to the widespread idea that the visitor should stay for eight days so that he can offer forty prayers in the Mosque is wrong. Although it says in some ahaadeeth “Whoever offers forty prayers therein Allaah will decree that he is safe from the Fire and free from hypocrisy,” this hadeeth is da’eef according to the scholars and cannot be taken as proof or relied upon. There is no set limit for visiting the Prophet’s Mosque. If a person visits for an hour or two, or a day or two, or for more than that, there is nothing wrong with that.
Fataawa Ibn Baaz, 17/406
Instead of this hadeeth we should look at the hasan hadeeth narrated by al-Tirmidhi (241) concerning the virtue of always being present for the opening takbeer of prayer in congregation. It was narrated that Anas ibn Maalik said: The Messenger of Allaah (peace and blessings of Allaah be upon him) said, “Whoever prays for forty days with the congregation, always being present for the first takbeer, it will be written that he will be safe from two things: he will be safe from the Fire and safe from hypocrisy.” Classed as hasan by al-Albaani in Saheeh al-Tirmidhi, 200.
The virtue mentioned in this hadeeth is general and applies to any mosque where prayers are offered in congregation, in any land; it does not apply only to al-Masjid al-Haraam [in Makkah] or al-Masjid al-Nabawi [in Madeenah].
Based on this, whoever consistently prays forty Days in congregation in which he is present for the first takbeer, it will be recorded that he will be safe from two things: safe from the Fire and safe from hypocrisy, whether the mosque is in Madeenah, Makkah or anywhere else.
And Allaah knows best.