Sixteen Afghan civilians, including three women and nine children, have been shot dead in their homes by a US soldier in a pre-dawn attack.
President Hamid Karzai condemned the slaughter on Sunday as "unforgivable" and furiously demanded an explanation from the US government.
"When Afghan people are killed deliberately by US forces this action is murder and terror and an unforgivable action," Karzai said in statement.
Senior US officials were scrambling to determine what caused the soldier to go on a shooting spree after leaving his base in southern Afghanistan, apparently heavily-armed and carrying night-vision equipment.
Officials confirmed that the soldier was being detained in Kandahar and that the military was treating at least five wounded people.
Barack Obama, the US president, said he was "deeply saddened" by the incident.
"I offer my condolences to the families and loved ones of those who lost their lives, and to the people of Afghanistan, who have endured too much violence and suffering," Obama said in a written statement.
One US official said the soldier, an Army staff sergeant, was believed to have acted alone and that initial reports indicated he returned to the base after the shooting and turned himself in.
General John Allen, the top US commander in Afghanistan, issued a statement pledging a "rapid and thorough investigation" into the shooting spree, and said the soldier will remain in US custody.
The US embassy in Kabul sent out an alert to its citizens in Afghanistan warning that as a result of the shooting "there is a risk of anti-American feelings and protests in coming days".
An AFP news agency reporter at the scene of the killings counted the bodies of 16 people. In one house, an elderly woman screamed: "May God kill the only son of Karzai, so he feels what we feel."
The shootings come at a particularly sensitive and critical time for the US, just as violence over the burning of Muslim holy books at a US base was starting to calm down. At least 41 people were killed in violence related to those protests.
Al Jazeera's Bernard Smith, reporting from Herat, said the soldier entered three houses near the base and opened fire on civilians.
"We are now being told by the police sources that the US soldier left his base at three o'clock this morning. It would have been pitch-black wherever he walked," our correspondent said.
"The soldier went through three separate houses, shooting at people as they slept in their beds. After the soldier shot these people, he turned himself in.
"It is frankly disastrous. It is not just a disaster for the people who were murdered and killed in their houses, it is disaster for the country I suspect."
Najeeb Azizi, a Kabul-based Afghan analyst, said the shooting will have deep repercussions on the already tenuous relations with the US.
“It is a very tragic incident in particular because the Afghan and US governments are trying to sign a strategic agreement for a long term," he said.
"A very bad message the Afghan people are getting - that if US military remains in Afghanistan beyond 2014 and their attitude and behaviour remains the same - of killing innocent civilians - what will be the consequences, and how will the Afghan people respond to it."
Source: Al Jazeera and agencies