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

Acting on a tip that dozens of ISIS-held hostages were about to be slaughtered, U.S. and Kurdish commandos stormed a prison in northeastern Iraq before dawn Thursday, rescuing the captives in a firefight that ended with an American soldier being shot to death, officials said.

The operation marked the first known instance of American service members battling ISIS fighters on the ground in Iraq under President Obama’s new mission to “train and advise” local forces against the terror group.

The U.S. soldier is the first to die in American combat operations against ISIS.

U.S. officials stressed that the raid did not signal a change in the mission, in which Obama has tried to avoid putting American “boots on the ground” in the battle against ISIS. Obama said as far back as February that American involvement could include rescue missions or the use of special operations forces.

The mission happened in northern Iraq. Google Maps

In this case, American military officials responded to a request from the Kurdish regional government in Iraq, which had learned that the hostages faced “imminent mass execution,” perhaps within hours, Pentagon spokesman Peter Cook said.

"In this support role, we're allowed to defend ourselves and defend partner forces and protect against the loss of innocent lives," Cook said. "And that’s what played out in this particular mission."

Secretary of Defense Ash Carter signed off on the 2 a.m. raid, in which America provided helicopters to transport the commandos to the prison in Hawija, 34 miles southwest of Kirkuk, officials said. When they hit the ground, Kurdish peshmerga fighters led the way, with backup from U.S. commandos, Cook said.

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

Related: ISIS Attacks Soared in Past 3 Months, Analyst Say

Even then, no one was sure who they'd find inside.

U.S. forces "didn't have a full accounting" of who the hostages were, Cook said.

Initially, American forces were "in an assist capacity on the perimeter of the target," according to a senior U.S. military official with direct knowledge of the raid. "However, when the firefight got so intense, U.S. moved in ... to protect the peshmerga and the hostages, so U.S. forces started killing ISIS themselves."

The official added that intelligence about the imminent execution was verified by the raid. "Mass graves were already dug,” the official said.

Several ISIS fighters were killed, and five were taken into custody and handed over to Kurdish authorities, Cook said.

The killed American, whom officials have not identified, suffered gunshot wounds during the battle, Cook said.

Four peshmerga fighters were wounded in the fight, he said.

U.S. military officials called the operation a success, though tempered by the loss of an American's life.

"We commend and congratulate the brave individuals who participated in this successful operation that saved many lives, despite the casualty," Gen. Lloyd Austin, commander of U.S. Central Command, said in a statement.

The commandos rounded up 75 hostages, a group that included more than 20 members of the Iraqi Security Forces. Fifty of the hostages were civilians and another five were members of ISIS who were being held prisoner by their own forces, a senior defense official told NBC News Thursday afternoon.

The official said that overhead surveillance discovered a series of trenches had been recently dug, signaling that a mass execution was going to take place. The hostages reported that their ISIS captors told them they would be executed after morning prayers today had they not been rescued only hours earlier.

A senior Kurdish official told NBC News that the prison was an ISIS commander's home that had been converted into a holding facility for suspected spies and turncoats.

Rescued hostages said in debriefings that prisoners had been executed at the facility prior to the raid, Cook said.

A senior U.S. defense official told NBC News that the Kurds initially believed that some of their peshmerga fighters were among those being held at the prison.

But it turned out that there was none.