Praise be to Allah.
The Sunnah is to offer the funeral prayer in a place that is just for funerals, outside the mosque, because of the report in al-Bukhaari (1245) from Abu Hurayrah (may Allaah be pleased with him) which says that the Messenger of Allaah (peace and blessings of Allaah be upon him) announced the death of the Negus on the day on which he died, then he went out to the prayer-place and lined them up in rows, and said takbeer four times.
But if the funeral prayer is offered in the mosque, there is nothing wrong with it, because of the report narrated by Muslim (973) which says that ‘Aa’ishah (may Allaah be pleased with her) issued orders that the bier of Sa’d ibn Abi Waqqaas be brought into the mosque and the funeral prayer be offered for him. The people objected to that, but she said: How quickly the people forget! The Prophet (peace and blessings of Allaah be upon him) did not offer the funeral prayer for Suhayl ibn al-Bayda’ except in the mosque.
Ibn Qudaamah (may Allaah be pleased with him)said in al-Mughni (2/186): There is nothing wrong with offering the funeral prayer for the deceased in the mosque if there is no fear that it may become contaminated. This is the view of al-Shaafa’i, Ishaaq, Abu Thawr and Dawood. End quote.
Shaykh Ibn ‘Uthaymeen (may Allaah have mercy on him) was asked: What is the correct opinion on the ruling on offering the funeral prayer for the deceased in the mosque?
He replied: The correct view is that there is nothing wrong with offering the prayer for the deceased in the mosque, because the Prophet (peace and blessings of Allaah be upon him) offered the funeral prayer for Sahl ibn Bayda’ in the mosque. End quote.
Majmoo’ Fataawa al-Shaykh Ibn ‘Uthaymeen (17/160).
And Allaah knows best.