A gunman has killed two American military advisers with shots to the back of the head inside a heavily guarded ministry building, and Nato has ordered military workers out of Afghan ministries as protests raged for a fifth day over the burning of copies of the Koran at a US army base.

The Taliban claimed responsibility for the Interior Ministry attack, saying it was retaliation for the Koran burnings, after the US servicemen - a lieutenant colonel and a major - were found dead on the floor of an office that only people who know a numerical combination can get into, Afghan and Western officials said.

The top commander of US and Nato forces recalled all international military personnel from the ministries, an unprecedented action in the decade-long war that highlights the growing friction between Afghans and their foreign partners at a critical juncture in the war.

The US-led coalition is trying to mentor and strengthen Afghan security forces so they can lead the fight against the Taliban and foreign troops can go home.

That mission, however, requires a measure of trust at a time when anti-Western sentiment is at an all-time high.

Afghan defence minister General Abdul Rahim Wardak called US defence secretary Leon Panetta to apologise for the shooting and offer his condolences, Pentagon press secretary George Little said in a statement.

"This act is unacceptable and the United States condemns it in the strongest possible terms," Mr Little said.

Security is tight in the capital, which is covered in snow, and foreigners working at the US Embassy and at international organisations have been banned from leaving their compounds.

US officials said they were searching for the assailant, who has not been identified by name or nationality.