In a statement read on state television, the ministry said that three cells had planned the attack on worshippers and security forces at the mosque as the Muslim holy month of Ramadan nears its climax.
The trapped would-be suicide bomber exchanged fire with the security forces, then set off explosives when he was surrounded in a house in the central Mecca neighbourhood of Ajyad al-Masafi near the mosque that had been used as the base for the attack, the ministry said.
The building collapsed, injuring six foreigners and five members of the security forces.
Earlier in the day, security forces had shot dead a wanted man at another suspected terrorist hideout in Mecca's al-Aseelah neighbourhood. The ministry also said a third cell had been broken up in the Red Sea city of Jeddah, but gave no further details.
Five suspected terrorists including a woman were arrested, it said.
Al Arabiya broadcast live footage of Muslim worshippers praying in the mosque, with no interruption.
It was not immediately clear who was behind the plot to attack the mosque, but ISIS, which wants to establish a theocratic caliphate ruled according to strict Islamic law, had in the past carried out attacks in the kingdom.
In May last year, Saudi security forces shot dead two alleged ISIS terrorists outside Mecca, and two others blew themselves up outside Mecca.
Get the latest election news, live updates and election schedule for Lok Sabha Elections 2019 on ndtv.com/elections. Like us on Facebook or follow us on Twitter and Instagram for updates from each of the 543 parliamentary seats for the 2019 Indian general elections.