Get breaking news alerts and special reports. The news and stories that matter, delivered weekday mornings.

Two U.S. service members were killed during a fierce battle with Taliban fighters in Afghanistan early Thursday — which local officials said resulted in the deaths of dozens of civilians.

The Americans were on a "train, advise and assist" mission led by Afghan forces when they came under heavy fire miles north of the restive city of Kunduz, according to U.S. and Afghan officials.

Said Mahmood Danish, spokesman for Kunduz Gov. Asadullah Omarkhel, told NBC News that Afghan special forces called in airstrikes to try to beat back the militants and that 30 civilians were killed in the fighting, many of them children.

According to Danish, four Afghan special forces members were killed and six others were injured, along with 26 Taliban fighters who were killed and 17 others who were injured. Speaking to The Associated Press, the Kunduz police chief, Gen. Qasim Jangalbagh, put the number of civilians who were killed at 26.

Let our news meet your inbox. The news and stories that matters, delivered weekday mornings. This site is protected by recaptcha

Senior U.S. military officials said that in addition to the two Americans who were killed, four others were injured, two of whom were soon able to return to duty.

A map showing the Afghan city of Kunduz. Google Maps

Two senior U.S. military officials told NBC News on Thursday that Afghan troops and U.S. military advisers were on a night raid against an unspecified Taliban target when they came under attack from gunfire after their helicopters landed in darkness. U.S. jets responded with air support, they said.

Army Brig. Gen. Charles Cleveland, a NATO spokesman, confirmed in an email to NBC News that the personnel called in airstrikes "to defend themselves."

"We take all allegations of civilian casualties very seriously," he said. "As this was an Afghan operation, we'll work with our partners to investigate."

The Afghan Defense Ministry was not available for comment.

Army Gen. John W. Nicholson, the U.S. commander in the country, mourned the deaths of the two U.S. personnel.

"Today's loss is heartbreaking and we offer our deepest condolences to the families and friends of our service members who lost their lives today," he said in a statement. "Despite today's tragic event, we are steadfast in our commitment to help our Afghan partners defend their nation."

U.S.-backed Afghan forces have been involved in fierce battles with a resurgent Taliban in and around Kunduz, which the Taliban briefly captured in late 2015. During the fightback, a U.S. AC-130 gunship mistakenly bombed a Doctors Without Borders hospital and killed at least 42 people, including at least 14 staff and 24 patients.

The Taliban fighters were eventually pushed out, but they attacked the city again last month and occupied several neighborhoods during intense fighting. Afghan forces said they fought back to regain control of the city.

Despite President Barack Obama's declaration that the Afghan combat mission over in 2014, U.S. warplanes have carried out more than 700 airstrikes this year — far surpassing the 500 of last year — according to Reuters. Nearly 9,000 U.S. personnel remain in the country.