The Taliban claimed responsibility for Saturday's shooting, saying it was in revenge for the burning of Korans at a US-run military base -- an incident that forced US President Barack Obama to apologise to the Afghan people.
In a day of violence across the country, a UN compound came under attack by thousands of demonstrators in northeastern Kunduz province, but they were driven back when police fired into the crowd, an AFP correspondent said.
Five people were reported killed in the attack, adding to the death toll from five days of often violent protests over the burning of Korans at the US-run Bagram airbase.
President Hamid Karzai issued a statement urging demonstrators and Afghan security forces to exercise restraint, saying the government was pressing Washington "on the need to bring to justice the perpetrators of the crime".
The two American military advisors from the International Security Assistance Force (ISAF) were in the interior ministry when "an individual" turned his weapon against them, NATO said, without giving further details.
A government source told AFP the two men were killed by a member of the Afghan police.
"For obvious force protection reasons, I have... taken immediate measures to recall all other ISAF personnel working in ministries in and around Kabul," said General John Allen, commander of NATO's International Security Assistance Force.
The Pentagon said the killings were "unacceptable" and called on Afghan authorities to better protect coalition forces and curtail raging violence.
The United States, which leads a 130,000-strong military force fighting the Taliban-led insurgency in Afghanistan, has advisors throughout the Kabul government.
Britain said its embassy was also temporarily withdrawing all civilian mentors and advisors from Afghan government institutions in Kabul.
The latest deaths followed the killing of two American troops on Thursday when an Afghan soldier turned his weapon on them at their base in eastern Nangarhar province as demonstrators approached.
The Koran burning has inflamed anti-Western sentiment already smouldering in Afghanistan over abuses by US-led foreign troops, such as the release last month of a video showing US Marines urinating on the corpses of dead Taliban.
Four French soldiers were shot dead by an Afghan army colleague at their base in Kapisa province in late January shortly after the video was released.
Violent anti-US protests have seen furious Afghans attack French, Norwegian, UN and US bases, shouting "Death to America" after the Taliban exhorted their countrymen to kill foreign troops to avenge the Koran burning.
There were fresh protests in five different Afghan provinces Saturday over the burning of the Islamic holy book at the US airbase at Bagram near Kabul.
In the assault on the UN compound in Kunduz, five people were killed and 66 wounded, including 11 police, health ministry officials and police said.
The UN Afghanistan mission issued a statement urging protestors to "reject calls to violence... in order not to allow the enemies of peace to take advantage of the situation".
In Mihtarlam, in the central province of Laghman, hospital officials told AFP 15 protesters had been brought in with gunshot wounds.
Rallies elsewhere in Afghanistan were largely peaceful, however, authorities said, with protesters chanting "Death to America" and "Long live Islam".