A suicide bomber blew himself up near the Grand Mosque in Mecca as police disrupted a plot to target the holiest site in Islam just as the fasting month of Ramadan ends, Saudi security forces said.

The Interior Ministry said it launched a raid around Jiddah, as well as two areas in Mecca itself, including the Ajyad Al-Masafi neighbourhood, located near the Grand Mosque.

There, police said they engaged in a shoot-out at a three-storey house with a suicide bomber, who blew himself up and caused the building to collapse. He was killed, while the blast wounded six foreigners and five members of security forces, according to the Interior Ministry's statement. Five other people were arrested, including a woman, it said.

Saudi state television aired footage after the raid on Friday near the Grand Mosque, showing police and rescue personnel running through the neighbourhood's narrow streets. The blast demolished the building, its walls crushing a parked car. Nearby structures appeared to be peppered with shrapnel and bullet holes.

The Interior Ministry said the thwarted "terrorist plan" would have violated "all sanctities by targeting the security of the Grand Mosque, the holiest place on Earth".

"They obeyed their evil and corrupt self-serving schemes managed from abroad whose aim is to destabilise the security and stability of this blessed country," it said.

The ministry did not name the group involved in the attack. The ultra-conservative Sunni kingdom battled an al Qaida insurgency for years and more recently has faced attacks from a local branch of the Islamic State group.

Neither group immediately claimed involvement, though IS sympathisers online have urged more attacks as an offensive in Iraq slowly squeezes the extremists out of Mosul and their de facto capital of Raqqa in Syria comes under daily bombing from a US-led coalition.

The disrupted attack comes at a sensitive time in Saudi Arabia. King Salman earlier this week short-circuited the kingdom's succession by making his son, Defence Minister Mohammed bin Salman, first in line to the throne.

The newly appointed 31-year-old crown prince is the architect of Saudi Arabia's stalemated war in Yemen against Shiite rebels. He has also offered aggressive comments about the kingdom confronting Shiite power Iran.

Iran's Foreign Ministry on Saturday condemned the Mecca plot and said it remains willing to work with other countries in confronting terrorism.

Meanwhile, Saudi Arabia and other Arab countries have cut diplomatic ties to neighbouring Qatar and are trying to isolate the energy-rich country over its alleged support of militants and ties to Iran. Qatar long has denied those allegations.

As the Interior Ministry announced the raid, more than one million Muslim faithful prayed at the Prophet's Mosque in Medina to mark the end of Ramadan. In July 2016, a suicide bombing there killed four members of Saudi Arabia's security forces.

Millions of Muslims from around the world visit the mosque, the burial site of the Prophet Muhammad, every year as part of their pilgrimage. The same day in July, separate suicide bomb attacks targeted a Shiite mosque in eastern Saudi Arabia and near the US Consulate in Jiddah.

The Grand Mosque has been the target of militants before, in part because it represents a symbol of the ruling Al Saud family's clout in the Islamic world. The Saudi monarch bears the title of "custodian of the two holy mosques".

In 1979, some 250 militants seized the mosque and held it for two weeks as they demanded the royal family abdicate the throne. When Saudi troops stormed the mosque, the official death toll was 229, including extremists and soldiers.