A gunman shot dead two Saudi guards and wounded three others at the gate of the royal palace in the Red Sea city of Jeddah on Saturday, the interior ministry said.
Royal guards killed the gunman, identified by the ministry as a 28-year-old Saudi national armed with a Kalashnikov and three grenades.
"An outpost of the royal guard came under fire by a person who got out of a Hyundai car," the ministry said in a statement carried by the official Saudi Press Agency.
"He was immediately dealt with and his cowardly act also resulted in the martyrdom" of two royal guards, it added.
The US embassy in Saudi Arabia had earlier cautioned its citizens over reports of the attack.
"Due to the possibility of ongoing police activity, American citizens are advised to exercise caution when travelling through the area," the embassy said in a brief statement.
The warning comes after the Saudi police raided hideouts of a "terror" cell linked to the Islamic State group this week, killing two people and arresting five others, according to the national security agency.
The State Security Agency said police raided three hideouts in the capital Riyadh and exchanged gunfire at one of them, the SPA news agency reported on Thursday.
Since late 2014, IS has claimed a series of bombings and shootings against Shiites and security forces in the Sunni-majority kingdom.
Saudi Arabia is a member of the US-led international coalition that has been battling the Sunni extremist group in Syria and Iraq.