Updated

President Hamid Karzai gave Barack Obama his first foreign policy test Wednesday, warning the president-elect that U.S. forces must stop killing civilians.

Villagers in the south said U.S. troops bombed a wedding party and killed 40 people, mostly children, and wounded 28 others, The New York Times reported.

The U.S. military said it was investigating, and a villager said American forces had given them permission to bury the dead, which he said included 23 children and 10 women. A U.S. spokesman added that "if innocent people were killed in this operation, we apologize and express our condolences."

The bombing of the remote village of Wech Baghtu in the southern province of Kandahar on Monday afternoon destroyed an Afghan housing complex where women and children had gathered to celebrate, villagers said. Body parts littered the wreckage and nearby farm animals lay dead.

Villager Abdul Jalil, a 37-year-old grape farmer whose niece was getting married, told an Associated Press reporter at the scene of the bombing that U.S. troops and Taliban fighters had been fighting about a half mile from his home.

Karzai's office said the attack killed about 40 people and wounded 28. The bodies were buried before the AP reporter arrived, and he could not verify the death toll.

Wedding parties in Afghanistan are segregated by gender, explaining why so many women and children could have died.

Karzai referred to the deaths at a news conference held to congratulate Obama on his election victory. But the Afghan leader used the occasion to press the president-elect to prevent civilian casualties.

"Our demand is that there will be no civilian casualties in Afghanistan. We cannot win the fight against terrorism with airstrikes," Karzai said. "This is my first demand of the new president of the United States — to put an end to civilian casualties."

Elsewhere in Kabul, Gen. David Petreaus, the new chief of U.S. Central Command, met with Afghan Defense Minister Gen. Abdul Rahim Wardak and assured officials that Obama's entry into the White House "will not change any commitment that the U.S. has already made to Afghanistan," Defense Ministry spokesman Gen. Mohammad Zahir Azimi quoted Petreaus as saying.

"Until Afghanistan can stand on its own feet the United States will help," Azimi quoted the U.S. general as saying.

Obama's victory was welcomed by many Afghans because of his pledges of commitment to Afghanistan, including sending more U.S. troops and boosting efforts to attack extremists along the Afghan-Pakistan border. Many observers expect the U.S. military to change its focus from Iraq to Afghanistan under an Obama administration.

Afghan officials attending a U.S. Embassy-sponsored party — held at a Kabul hotel attacked by suicide bombers in January — to watch election returns early Wednesday overwhelmingly voted for Obama over Sen. John McCain in a mock election. The final result: 74-3.

Karzai told a news conference that he hopes the election will "bring peace to Afghanistan, life to Afghanistan and prosperity to the Afghan people and the rest of the world." He applauded America for its "courage" in electing Obama.

But a statement from the president's office later Wednesday again condemned the civilian deaths and urged military forces to avoid Afghan villages and instead concentrate on the "sources" of terrorism, a clear reference to Pakistan.

Civilian casualties, which undermine popular support for the Afghan government and the international mission, have long been a point of friction between Karzai and the U.S. or NATO. According to an AP count of civilian deaths this year, U.S. or NATO forces have killed at least 275 civilians, while 590 have died from militant-caused violence like suicide bombs.

The airstrikes in Kandahar come only three months after the Afghan government found that a U.S. operation killed some 90 civilians in the western Afghan village of Azizabad. After initially denying any civilians had died there, a U.S. report ultimately concluded that 33 civilians were killed.

Following that operation, Karzai said relations between Afghanistan and the United States were seriously damaged.

Jalil said American forces came into his village late Monday night or Tuesday morning — after the bombing run — and searched the villagers and detained some men. Jalil said he told the Americans that they could search his vineyards and his home but that they wouldn't find any militants.

"The Americans came and told us, 'You are sheltering the Taliban,' and I told the Americans 'Come inside and see for yourself, you are killing women and children,"' Jalil said. "After they saw that all the dead were civilians, they gave us permission to bury the bodies."

The U.S. military said it had sent personnel to the site to assess the situation and take appropriate action.

"Though facts are unclear at this point, we take very seriously our responsibility to protect the people of Afghanistan and to avoid circumstances where noncombatant civilians are placed at risk," Cmdr. Jeff Bender said in a statement. "If innocent people were killed in this operation, we apologize and express our condolences to the families and the people of Afghanistan."

Another witness to the bombing, Mohammad Nabi Khan, told AP at the main hospital in Kandahar city that two of his sons, ages 4 and 11, and his wife's brother were among the dead.

"What kind of security are the foreign troops providing in Afghanistan?" he asked.

The Associated Press contributed to this report.