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.