A Jordanian police officer opened fire at a police training centre in the Jordanian capital, killing two Americans, two Jordanians and a South African, before being shot dead, the government said.
A US official said at one point that the death toll had apparently risen to eight, but Jordanian government spokesman Mohammed Momani disputed that figure.
The attack also wounded seven people, including two Americans, three Jordanians and a Lebanese, Mr Momani said.
He said authorities are investigating whether the attacker's motive was personal or political.
Jordan, a close US ally that has a peace treaty with Israel, has long been seen as an island of relative stability in a turbulent region. Over the past year, the pro-Western kingdom has taken a high-profile role in the fight against extremists, including the Islamic State group, which controls large areas of neighbouring Iraq and Syria.
There has been concern that militants could carry out revenge attacks on Jordanian soil.
Fayez Dwairi, a strategic analyst and former senior military officer, said he could not recall such a deadly shooting inside a Jordanian security compound in his 36 years in the military.
The government did not release the identity of the attacker, but a former member of parliament said authorities told him the shooter was 29-year-old Anwar Abu Zaid.
His brother Fadi told the Associated Press that Abu Zaid was mentally stable and "not an extremist at all".
He said his brother, a father of two, joined the security forces at the age of 18, had been working at the training centre for several months and had left for work as usual on Monday morning. Fadi Abu Zaid said the family was demanding to know about the circumstances of the death and would not accept his body until the authorities release more information.
In Washington, President Barack Obama said the attacker had been dressed in a military uniform.
"We take this very seriously and will be working closely with the Jordanians to determine exactly what happened," he said during an Oval Office meeting with Israeli prime minister Benjamin Netanyahu.
US forces in Afghanistan have come under attack on a number of occasions by local police and troops serving alongside them, in what are known as "green-on-blue" assaults. Such attacks have been extremely rare in the Middle East.
A US official said the Americans who were killed and wounded in the attack were part of a State Department police training programme. The official said all were civilians, but declined to identify them.
Mr Momani said the Jordanian killed in the attack was a civilian employee at the centre.
He said the wounded were being treated at Jordan's main military hospital and were visited by King Abdullah II.
Why are you making commenting on The Herald only available to subscribers?
It should have been a safe space for informed debate, somewhere for readers to discuss issues around the biggest stories of the day, but all too often the below the line comments on most websites have become bogged down by off-topic discussions and abuse.
heraldscotland.com is tackling this problem by allowing only subscribers to comment.
We are doing this to improve the experience for our loyal readers and we believe it will reduce the ability of trolls and troublemakers, who occasionally find their way onto our site, to abuse our journalists and readers. We also hope it will help the comments section fulfil its promise as a part of Scotland's conversation with itself.
We are lucky at The Herald. We are read by an informed, educated readership who can add their knowledge and insights to our stories.
That is invaluable.
We are making the subscriber-only change to support our valued readers, who tell us they don't want the site cluttered up with irrelevant comments, untruths and abuse.
In the past, the journalist’s job was to collect and distribute information to the audience. Technology means that readers can shape a discussion. We look forward to hearing from you on heraldscotland.com
Comments & Moderation
Readers’ comments: You are personally liable for the content of any comments you upload to this website, so please act responsibly. We do not pre-moderate or monitor readers’ comments appearing on our websites, but we do post-moderate in response to complaints we receive or otherwise when a potential problem comes to our attention. You can make a complaint by using the ‘report this post’ link . We may then apply our discretion under the user terms to amend or delete comments.
Post moderation is undertaken full-time 9am-6pm on weekdays, and on a part-time basis outwith those hours.
Read the rules here