Hassan Abu Ahmed, 46, was held for months by Islamic State militants before being freed in a dramatic raid by U.S. and Kurdish special forces. (Ayman Oghanna/For The Washington Post )

Before Kurdish and U.S. commandos arrived at his cell door last month, Hassan Abu Ahmed had not seen daylight for five months.

Held in an Islamic State prison near the northern Iraqi town of Hawijah, the 46-year-old was accused of being a spy and regularly tortured.

Sometimes a plastic bag was placed over his face until he nearly suffocated. He was electrocuted, beaten with plastic pipes and whipped with electric cable. There were mock executions.

“They’d put a gun to your head and say we are going to kill you now, and then shoot next to you,” Abu Ahmed said. “We always feared execution.”

Two prisoners held in the facility described life in an Islamic State prison and the mission that set them free. The operation, which a Kurdish commando also detailed, freed 69 detainees and was the first confirmed time that U.S. troops had directly accompanied Iraqi forces on the battlefield in the fight against the Islamic State.

But the raid also claimed the life of a U.S. soldier, the first killed in combat since troops returned last year, highlighting the cost the United States faces amid a Pentagon push to expand military involvement in Iraq and Syria. Defense Secretary Ashton B. Carter recently summed up the new U.S. strategy against the Islamic State as the “three R’s”: Raqqa, Ramadi and raids. Raqqa is the group’s stronghold in Syria, and the Iraqi city of Ramadi was captured by the militants earlier this year.

The raid in Hawijah was supposed to be led by Kurdish forces, but U.S. troops were called in when they were pinned down.

Kurdish authorities had received intelligence that captured Kurdish soldiers, known as peshmerga, were being held at the makeshift prison. A mass grave measuring about 20 yards long had been dug on the prison’s grounds, and it was feared the detainees would soon be killed.

The operation was planned over five days, said a Kurdish commando who participated in the raid and spoke on the condition of anonymity, citing security protocol. The United States was asked to play a “logistics and support” role.

On Oct. 21, the day before the raid, Abu Ahmed said he heard drones buzzing overhead from the cell he shared with dozens of other prisoners who had run afoul of the Islamic State’s draconian laws.

Abu Ahmed had been accused of supplying information to Kurdish authorities, something he admits he had done.

Qassim Awad, 36, a former soldier and truck driver, had been in prison for about two weeks before the raid by U.S. and Kurdish forces. (Ayman Oghanna/For The Washington Post)

“I was giving information to people here because we wanted our areas to be liberated,” he said. “Eyewitness observation of what was happening in the area, numbers of Daesh people, whether they had any intention to attack,” he said, using an Arabic term for the Islamic State.

Abu Ahmed’s frequent trips to the nearby city of Kirkuk, outside the Islamic State’s territory, had raised suspicions. When the militants seized control of Hawijah, about 150 miles north of Baghdad, in June 2014, he had resisted pressure to join them.

“They knew me personally very well, my family, my tribe,” he said. “They were looking for influential people to join.”

Life in the town went from “white to black” when the militants took over, he said.

In May, five armed Islamic State members appeared at his house to arrest him. After he was blindfolded and driven to prison that day, he did not see daylight for another five months.

He said he prayed for rescue but had little hope it would come.

But at 2 a.m. on Oct. 22, special forces from the Kurdish region’s Counter Terrorism Department launched their mission — accompanied by about 30 U.S. troops. U.S. Chinook and Black Hawk helicopters also took part.

Abu Ahmed heard the helicopters draw closer. When the guards started shouting, he realized that a rescue attempt was underway.

“Paratroopers, paratroopers!” the guards yelled in the confusion, he said. U.S. and Kurdish officials say no paratroopers were involved, with helicopters landing about 30 yards from the prison.

In another cell, Qassim Awad, 36, was jolted awake by the shouting guards.

“We heard the helicopters,” he said. “We were all scared and went to the bathroom and hid.”

Awad said he was in prison for attempting to escape Hawijah with his two wives and 10 children. He had been in prison for about two weeks before the rescue attempt.

The prisoners’ accounts of why they were held could not be independently verified. A Kurdish security officer was present during their interviews, which took place in a security building in Irbil. Two weeks after the raid, the former detainees are still being processed by Kurdish and U.S. authorities, with concerns that some may be Islamic State sympathizers who were arrested for minor offenses.

Awad said the prison’s main interrogator, a militant from neighboring Diyala province, was called “Abu Hajjar.” Awad, a truck driver who had served in the Iraqi military, said he lost some of his hearing after being beaten on the head with a plastic pipe.

“I confessed,” he said. “I couldn’t handle the torture. Other prisoners told me about the electric shocks, and I couldn’t handle it. I decided death was the best way out, but then they came.”

Awad had put his fingerprint to a written confession, though he doesn’t know what it said because he was blindfolded at the time, he said.

He said he had been told that he was awaiting judgment from an Islamic State court, but he was expecting to be executed.

On the night of the raid, the Kurdish commando said his team came under fire from the prison guards as soon as the helicopters landed. The plan was for Kurdish forces to take the lead.

“They attacked us straight away,” he said. “At that stage we were leading the offensive.” The Kurdish forces fought back, killing two guards, he said. His team attempted to enter one of several buildings in the prison, formerly a house.

But then they came under fire again. “We were involved in an intense firefight so we requested support [from the Americans],” the commando said. “During that intense firefight, it was unfortunate that the American officer was martyred.”

When the U.S. forces and Kurdish forces reached Awad’s cell, he said, one prisoner who spoke some English cried out, “Please help us! Please help us!” The prisoners were searched and handcuffed before being sat on the grounds to wait to be airlifted out.

Kurdish forces searched for their peshmerga colleagues, but none were found.

Six Islamic State militants were arrested, according to Kurdish authorities, and 20 were killed. In a video released in the aftermath, the Islamic State said just six militants had held off the attack, which it described as a “failed operation.” Twenty-five prisoners were killed, it claimed. The video showed four peshmerga being executed on the bombed remains of the compound in retaliation for the raid.

“Obama, you have learned a new lesson,” a masked Islamic State militant said in the video. “You did not gain anything, you returned to your bases, and with losses and humiliation.”

The American killed was Master Sgt. Joshua L. Wheeler, who had served 14 tours in Iraq and Afghanistan. He was 39.

His actions and those of one of his colleagues protected those who were involved in breaching the compound and made the mission successful, Carter later said.

“He was a capable officer. He will leave behind a gap,” said the Kurdish commando.

Read more:

The damage Islamic State has done to ancient sites in Iraq and Syria

Today’s coverage from Post correspondents around the world