US officials warned of reprisals after the soldier went on a rampage in villages near a base in Kandahar. Nine children were among those killed.
President Barack Obama phoned his Afghan counterpart Hamid Karzai to express condolences. But President Hamid Karzai has said the massacre is “unforgivable”.
US Defence Secretary Leon Panetta has said a full investigation is under way.
The soldier, believed to be a staff sergeant, is reported to have walked off his base at around 03:00 a.m. on Sunday.
In the villages of Alkozai and Najeeban, about 500 m from the base, he reportedly broke into three homes.
At one house in Najeeban, 11 people were found shot dead, and some of their bodies set alight. At least three of the child victims are reported to have been killed by a single shot to the head.
The soldier is being detained in Kandahar and the military is treating at least five people wounded in the attacks, officials said.
The detained soldier has not been identified, although US officials quoted by the Associated Press news agency said he was from Joint Base Lewis-McChord in Washington state, 38 years old, married with two children, and had served three tours in Iraq and was on his first deployment in Afghanistan.
The killings come amid already high anti-US sentiment in Afghanistan following the burning of Korans at a NATO base in Kabul last month.
US officials have repeatedly apologized for the incident but they failed to quell a series of protests and attacks that killed at least 30 people and six US troops.
However, the latest incident has damaged already fragile relations between Kabul and Washington.
The Taliban is using the shooting as a propaganda victory, placing President Hamid Karzai in a difficult position.
Angry tribal elders are now demanding an immediate end to US night raids on Afghan homes.
Afghan MPs passed a strong resolution to condemn the killing and demanded an open trial on Afghan soil. However, Afghanistan has signed an agreement with NATO that foreign soldiers should be tried in their own countries.
The killings could further fuel calls for a more rapid withdrawal of US troops from Afghanistan.
Meanwhile, US personnel in Afghanistan were warned of possible reprisal attacks.
“The US Embassy in Kabul alerts US citizens in Afghanistan that as a result of a tragic shooting incident in Kandahar province involving a US service member, there is a risk of anti-American feelings and protests in coming days, especially in the eastern and southern provinces,” the embassy said in an emergency statement on its website.
In a statement released by the White House on Sunday, President Obama said: “This incident is tragic and shocking, and does not represent the exceptional character of our military and the respect that the United States has for the people of Afghanistan.”
Afghan officials also fear there will be violent demonstrations and have deployed extra police and troops around Kandahar.
President Hamid Karzai described the killings as the “intentional killing of innocent civilians” and said they could “not be forgiven”.
This is the first time Afghan civilians have been targeted by foreign soldiers in this way.
However, one US soldier was convicted last year on three counts of premeditated murder after leading a rogue “kill team” in Afghanistan.
A recent poll by ABC News and The Washington Post found 60% of Americans believe the war in Afghanistan is not worth its costs. Nearly the same number advocated an early US pullout from the country.
On a previously unannounced trip to Afghanistan, Germany’s Chancellor Angela Merkel said she could not be sure German troops would withdraw by 2014 as originally planned, but they were working towards that target.
The US House Ethics Committee has voted to release its report on former Republican Representative…
ABC News has agreed to pay $15 million to President-elect Donald Trump to settle a…
South Korea’s parliament has voted to impeach President Yoon Suk Yeol over his failed attempt…
Israeli war planes have carried out more than 100 air strikes in Syria on December…
President-elect Donald Trump has threatened to impose 100% tariffs on the BRICS countries if they…
Syrian troops have withdrawn from the city of Aleppo following an offensive by rebels opposed…