Suspected al-Qaeda militants have killed 22 people - mostly foreigners - in the eastern Saudi city of Khobar, the interior ministry in Riyadh says.
Saudi special forces stormed the compound
The 25-hour crisis ended on Sunday, when Saudi commandos stormed a housing complex where militants had been holding dozens of people hostage.
Some of the captives were killed during the raid, but most are said to be safe.
Three of the militants escaped and a fourth - described as the ringleader - was arrested, the ministry said.
Its list of foreigners killed include people from Britain, Egypt, India, Italy, the Philippines, South Africa, Sri Lanka, Sweden, and the US.
It is not clear whether the ministry total includes any foreigners killed during the commando operation.
One South African
Two Sri Lankans
Twenty-five people of different nationalities were wounded, the interior ministry added.
The crisis began on Saturday, when the gunmen went on a shooting spree in Khobar.
They first attacked company offices, killing a number of people. The body of one Westerner was tied to a car and dragged through the streets.
The militants then moved to the Oasis housing compound, where they seized several dozen people - mostly expatriates.
The gunmen apparently tried to separate the Muslims from the non-Muslims, releasing five Lebanese nationals in the first few hours.
On Sunday morning, commandos were dropped by helicopter onto the roof of the housing block and moved in.
BBC Middle East correspondent Paul Wood says it was a huge gamble, but Saudi officials had little choice as the gunmen had begun executing hostages.
One survivor said some captives had tried to escape, but were caught and had their throats cut.
The Saudi ambassador to Britain, Turki al-Faisal, told the BBC that the bodies of nine hostages had been found on the premises when forces went in.
A statement purported to be from an al-Qaeda-linked group and posted on an Islamic website, said its militants had "slaughtered" an Italian and a Swedish hostage.
The claim could not be verified but was consistent with witness accounts.
An earlier statement purporting to come from an al-Qaeda-linked group claimed responsibility for the attack.
The incident is the latest in a series of attacks on the kingdom's oil industry - the world's largest.
Our correspondent says that with oil at more than $40 a barrel, the attack is bad news for the world's economy.
He adds that by terrifying foreign workers in the oil industry, the militants are also undermining the ruling Saudi royal family - one of their stated aims.
Khobar, 400km (250 miles) north-east of Riyadh, is one of the centres of the Saudi oil industry, in which foreigners play a key role.
In early May, five foreigners were killed in an attack on a petrochemical site in the city of Yanbu.
The Saudi government launched a high-profile assault on militants following a triple bombing in Riyadh in May last year, which killed 35 including nine bombers.