DUBAI (Reuters) - Saudi security forces on Friday foiled a suicide attack on the Grand Mosque in the Muslim holy city of Mecca, cornering the would-be attacker in an apartment, 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 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 neighborhood 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 Islamist militant hideout in Mecca’s al-Aseelah neighborhood. The ministry also said a third cell had been broken up in the Red Sea city of Jeddah, but gave no further details.

Slideshow ( 4 images )

Five suspected militants including a woman were arrested, it said.

The month of fasting ends with the Eid-al-Fitr holiday, expected to be on Sunday. Saudi monarchs usually spend the last 10 days of Ramadan in Mecca.

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 the Islamic State group, 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 Islamic State fighters outside Mecca, and two others blew themselves up outside Mecca.