Shouting "God is Greatest" the gunmen killed two Americans, two Britons and an Australian working for the Swiss-based ABB Lummus in the Red Sea city, the first attack on an oil facility in the world's largest oil exporter.
Saudi Ambassador to Britain Prince Turki al-Faisal said he thought al Qaeda was behind the attack. There were reports a sixth man was shot near a hotel and his body dragged through the town.
With Saturday's attack, terrorists appear to have expanded their targets to include vital economic facilities in the kingdom, which until now has seen a spate of bombings on residential compounds and government security symbols.
"Three employees entered the site, taking advantage of their passes and allowing a fourth accomplice to enter through the emergency gate. They opened fire at offices, using various weapons and fled the spot to a residential complex with the aim of attacking it," a ministry official said in a statement.
Police chased the four terrorists through residential areas and guards at a residential compound opened fire on them, forcing them to retreat. They then exchanged fire with police in several spots. Three of the terrorists were killed and the fourth was wounded and died later.
The ministry did not name the four. It said the attack on the ABB engineers also wounded three others - a Pakistani, an American and a Canadian. One national guard died in the clashes. Eight others and 10 policemen were wounded.
Other Saudi officials said two Saudi officers were killed.
An oil official said the gunmen shouted "God is Greatest" as they fired. The term is often used by al Qaeda fighters.
But Saudi Arabia's de facto ruler Crown Prince Abdullah said "Zionists' hands" were behind Saturday's attacks.
Local witnesses said terrorists first shot dead a Western man as he was leaving his compound near the Holiday Inn Hotel in Yanbu, tied his body to their car and dragged it into the streets. His nationality was not known. Other reports said the body belonged to one of the five engineers. None of the embassies could confirm the report.
Terrorists also shot at US fast food chain McDonald's during the rampage in Yanbu, 250km north of Jeddah, which had been largely unaffected by violence.
An ABB Lummus spokesman said two other Americans were wounded in Yanbu, which along with the port of Jubail is home to much of Saudi's oil refining and petrochemical industries.
ABB Lummus is carrying out an upgrading project for Saudi petrochemical firm YANPET, jointly owned by US Exxon Mobil and Saudi Basic Industries Corp (SABIC).
The US State Department condemned the attack and said Washington was working with the Saudis to determine the facts.