A gunman on Saturday drove up to a gate of the king's palace in the Saudi city of Jeddah and opened fire, killing at least two security guards and wounding three others before being shot dead, according to officials.
The attacker, identified by the interior ministry as Mansour al-Amri, a 28-year-old Saudi national, was armed with a Kalashnikov rifle and three Molotov cocktails.
Amri disembarked from his vehicle at a checkpoint outside the western gate to the Al-Salam Palace and started shooting, according to the Saudi Press Agency.
Royal guards shot him dead on the spot.
The palace, where the royal family conducts official business during the summer months, is located next to the King Abdulaziz Road and Andalus Road on the waterfront of the Red Sea coastal city.
Saudi King Salman is currently outside the kingdom on a state visit to Russia.
Amri did not have a criminal record or any known connection to armed groups, said Interior Ministry spokesman Mansour al-Turki, speaking by phone to al-Arabiya television.
An investigation was under way to determine his motive for the attack, added Turki.
Earlier on Saturday, the United States Embassy in Saudi Arabia had warned US citizens to exercise caution in the area around the palace after reports on social media of an attack there.
"Due to the possibility of ongoing police activity, American citizens are advised to exercise caution when travelling through the area," the embassy had warned.
Attackers have targeted Shia mosques in the country's eastern provinces where much of its Shia minority is based, as well as security forces.
This week, Saudi police raided hideouts of a "terror" cell linked to ISIL, killing two people and arresting five, according to the national security agency.
The State Security Agency said police raided three hideouts in the capital, Riyadh, and exchanged gunfire in one of them, the SPA news agency reported on Thursday.es in the country's eastern provinces where much of its Shia minority is based, as well as security forces.
In July this year, Saudi authorities said they foiled an attack on the Grand Mosque in the Muslim holy city of Mecca, which they blamed on ISIL.
Hundreds of Saudi citizens have travelled to Syria to join the group.