Police cornered the would-be terrorist in an apartment outside the mosque compound, where he blew himself up, the Interior Ministry said.
In a statement read on state television, the ministry said that three cells had planned the attack on worshippers at the mosque as the Muslim holy month of Ramadaan nears its climax.
The trapped terrorist exchanged fire with the security forces, then set off explosives when he was surrounded in a house in the Ajyad al-Masafi neighbourhood 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.
SCENE: A previous bombing
Earlier in the day, security forces had shot dead a wanted man at another suspected terrorist hideout in the al-Aseelah neighbourhood.
Also, a third cell had been broken up in Jeddah, and five suspected terrorists including a woman were arrested, the ministry said.
Saudi royals usually spend the last 10 days of Ramadaan in Mecca.
Countries in the northern hemisphere that sighted the new moon on Saturday celebrated Eid-al-Fitr yesterday, while those in the southern hemisphere will celebrate the holiday today.
It was not immediately clear who was behind the plot to attack the Ka’aba.
In May last year, Saudi security forces shot dead two alleged Islamic State fighters outside Mecca, and two others blew themselves up outside Mecca.