JALALABAD, Afghanistan — Militants affiliated with the Islamic State group stormed a prison in eastern Afghanistan in a daylong siege that left at least 39 people dead, including the assailants, and freed nearly 400 of their fighters before security forces restored order, a government official said Monday.

The attack underscored that the Islamic State affiliate in Afghanistan is still a formidable presence, and it highlighted the challenges ahead as U.S. and NATO forces begin to withdraw following Washington’s peace deal with the Taliban.

The peace accord aims to recruit the Taliban to battle the militants from IS, which U.S. officials have told The Associated Press is the Americans’ biggest foe in Afghanistan. Both the Taliban and the IS affiliate are staunch rivals.

At least 10 of the dead were IS militants involved in the assault to free their comrades from the prison in Jalalabad, the capital of Nangarhar province, said Ajmal Omar, a provincial council member. The rest of the dead were believed to be prisoners, civilians and Afghan forces, although no official breakdown was given.

Another 50 people were wounded in the attack that began Sunday when a suicide bomber detonated his explosives-packed vehicle at the entrance to the prison complex some 70 miles east of Kabul, Omar said.

Other militants simultaneously stormed the prison and took up positions in nearby residential buildings. They fired on Afghan troops for several hours, even after the security forces retook the prison and began recapturing some of the inmates.

Of the five prisoners killed by the militants, at least three were members of the Taliban, showing the tensions between the two factions.

An AP photographer who went inside the prison, which houses about 1,500 inmates, said he saw the bodies of five of the attackers and at least four prisoners.

Omar said about 430 prisoners stayed in their cells or hid during the siege. It was unclear Monday how many prisoners remained at large.

One inmate, who gave his name as Azizullah, said he heard the powerful explosion, followed by the militants storming into the main prison block, shouting for their fighters and telling them to flee.

“The aim of this attack was to rescue all Daesh members inside the prison, and unfortunately that included five or six senior Daesh people,” Omar said, using an Arabic acronym for the Islamic State group, which is also known as ISIS. Among the prisoners who escaped was the group’s shadow governor for neighboring Kunar Province, he said.

The Islamic State affiliate in Afghanistan, known as IS in Khorasan Province, claimed responsibility for the attack. The militants have established a base in Nangarhar Province.

Comments are not available on this story.