Insurgents opened fire on an oil refinery co-owned by ExxonMobil and the Saudi company SABIC in the kingdom’s north-western town.
The dead included two Americans, two Britons, an Australian and a Saudi, company officials and diplomats said.
Earlier reports suggested three Americans had been killed.
Interior Ministry officials said three attackers were also killed.
“At 7:00 am (0400 GMT) on Saturday four men entered the headquarters of a Saudi contractor in Yanbu and fired at random on Saudis and foreigners,” the Interior Ministry said in a statement.
“Security forces chased them and they took refuge in residential areas and hijacked some cars. Three of them were killed and a fourth was wounded. There were a number of deaths and injuries among Saudis and foreigners,” it added.
US diplomats in Saudi Arabia also said they had received reports that Westerners, including Americans, had been attacked in Yanbu and some were killed.
At least eight people were killed
A hotel employee in Yanbu confirmed a Westerner was killed in a five-star hotel in the city.
Saudi Arabia has been battling a year-long wave of Islamist insurgency, targeting mainly foreigners.
More than 50 people have been killed in bombings in the capital, Riyadh, including an attack on a Saudi security building last month. At least eight people, including a senior security officer, were killed in that blast.