The attack was being planned by three terrorist groups, two based in Mecca and the third in nearby Jeddah, a report by Saudi Arabian broadcaster Al Arabiya said, citing Saudi security forces.
The attackers planned to target Muslim worshippers at the mosque, Al Arabiya said.
As security forces closed in on the suspects in a district of Mecca, a suicide bomber opened fire on security forces and refused to comply with calls to turn himself in. He later reportedly blew himself up.
Six people were injured in the blast, while five members of security forces were "lightly injured".
Security forces arrested five people, including one woman, who are now being investigated.