Eight foreign United Nations workers have been killed in Afghanistan by protesters angered by a Koran burning in the US, in the deadliest attack on the UN there since the 2001 invasion.

Those killed included five Nepalese and three other international staff, a senior UN official said on condition of anonymity after the attack in which a mob overran the United Nations compound in the normally relatively safe city.

Sweden said one of its nationals was killed, while Norway said a Norwegian army officer was thought to be among the victims. The Taliban claimed responsibility for the violence.

US President Barack Obama condemned the attack ‘in the strongest possible terms’, while UN chief Ban Ki-Moon said it was ‘an outrageous and cowardly attack against UN staff which cannot be justified under any circumstances’.

A police spokesman said demonstrators seized weapons from guards at the UN building before turning them on the staff.

The Nepalese Gurkha guards killed several protesters before they were fatally wounded, the UN official said.

Part of a UN building was burned down in a battle that raged for over three hours, with small arms fire and explosions heard, an AFP correspondent at the scene said, as hundreds demonstrated in the streets.

Local police spokesman Lal Mohammad Ahmadzai said that ‘two of the killed UN staff were beheaded’ and that Taliban militants had infiltrated protesters.

But police official General Abdul Rauf Taj said that ‘according to the initial reports... none have been beheaded. They were shot in the head.’

Sweden's foreign minister said 33-year-old national Joakim Dungel was among those killed, while Norway's army said one of its officers had ‘in all likelihood’ died.

Balkh provincial governor Atta Mohammad Noor said at least 20 were wounded and 20 people have been arrested over the attack.

Mazar-i-Sharif is one of seven areas chosen by President Hamid Karzai and the international coalition to launch a transition process in which foreign forces will pass on responsibility for security to Afghan forces from 1 July.

Foreign forces have traditionally been less visible in the city, deemed relatively safe, than in other areas.

Ahead of Friday's violence Afghanistan had condemned the ‘disrespectful and abhorrent’ burning of the Koran by evangelical preacher Pastor Wayne Sapp in a Florida church, calling it an effort to incite tension between religions.

President Hamid Karzai called on the US to bring those responsible for the burning of the Islamic holy book on 21 March to justice.

Sapp set light to a Koran under the supervision of Terry Jones, who last year drew condemnation over his aborted plan to burn a pile of the holy books to mark the anniversary of the 11 September, 2001 terrorist attacks on the US.

Friday's protest began after the traditional prayers, and demonstrators gained access to the UN headquarters around 3pm, a police spokesman said.