KABUL — A sophisticated attack on a major prison in eastern Afghanistan that began Sunday night was finally shut down Monday evening, after scores were killed or injured and hundreds of prisoners escaped.

The main assault on the prison in Jalalabad, Nangahar's provincial capital, lasted more than 18 hours, and a clearing operation continued into the night Monday. At the tail end of the operation, led by Afghan special forces, suicide bombers inside the complex detonated their explosive vests.

At least 29 people, including civilians, security forces and inmates, were killed and 49 wounded in the course of the raid, according to the provincial governor's office. Those tolls are expected to rise as bodies are recovered from the prison.

The Islamic State asserted responsibility for the assault, the most ambitious operation the group has mounted in Afghanistan since it officially established a branch in the country in 2015. It follows the announcement Saturday by Afghanistan's main intelligence agency that the militant group's intelligence director had been killed near Jalalabad.

The attack also comes at a tense moment, as pressure builds to bring the Taliban and Afghan government together for peace talks. It began just hours before a confidence-building cease-fire was set to end, and the complex planning it entailed prompted suspicions among Afghan officials of Taliban involvement despite a statement from the group denying responsibility.

Bait and switch

The assault started when a car bomb targeting the prison's main gate detonated just after 6:30 p.m. Sunday, killing three policemen. But as Afghan security forces rushed to the scene, one group of gunmen took over a high-rise building abutting the complex while a second group launched another attack from the back of the prison, using rocket-propelled grenades to blast through a perimeter wall bordering a residential neighborhood.

"The car bomb was just a distraction," said a local Afghan security official who was at the scene. "There is no way they could have entered through the main gate. It's too fortified. The back of the prison, that was the weak point."

The official, who spoke on the condition of anonymity because he was not authorized to speak to the media, said it is thought the attack was carried out by 20 to 30 men. He said the relatively small group was able to take over the sprawling complex because the men coordinated with prisoners inside. "They knew exactly where they were going," he said. The gunmen overpowered the prison guards, began dividing up prisoners and raided the complex's weapons stores, handing weapons out to some of the prisoners.

A second Afghan official, who also spoke on the condition of anonymity, confirmed that account.

Hekmatullah, a prisoner wounded in the attack, said that after the initial explosion, prison guards locked the inner gates, but that just an hour later, gunmen in camouflage uniforms managed to enter.

The assailants started yelling at the prisoners, "Leave or we will shoot you," said Hekmatullah, who like many Afghans goes by one name. Once the assailants began firing, prisoners ran in all directions, and that's when he was shot, he said. He was taken to a hospital for treatment.

Once inside the prison, the gunmen raised an Islamic State flag over one of the towers, Hekmatullah said.

The prison held more than 1,700 inmates at the time of the attack, including fighters with links to the Islamic State and the Taliban, as well as criminals, according to the first official. As of Monday evening, more than 330 inmates remained at large, he said.

The assault quickly turned into a standoff because the heavily armed militants perched atop the nearby high-rise had a direct line of sight to the complex’s main entrance and courtyard. Afghan forces were able to enter the prison only after they cleared the packed high-rise, which houses shops, offices and apartments.

Even then, they had to move slowly because intelligence reports warned that the attackers planned to detonate explosives once government forces entered the prison.

After the blasts that rocked the complex during the clearing operation, the bodies of six suicide bombers were recovered.

“The attackers knew their plan. It was very sophisticated,” the first official said.

Disputed claims

Afghan officials have repeatedly alleged links between the Taliban and the Islamic State, claiming that the groups aid each other in staging attacks and share training pipelines. After the Jalalabad assault, Afghanistan’s interior minister echoed those allegations in a tweet, saying that when the Haqqani network and Taliban fighters carry out attacks that do “not suit them politically they rebrand it under” the Islamic State.

While evidence of direct links is flimsy, the long-held theory highlights the depths of distrust between the Afghan government and the Taliban movement and threatens progress toward formal peace talks.

The Jalalabad prison operation was almost identical to past Taliban attacks. A Taliban raid on a prison in Ghazni in 2015 that freed more than 350 prisoners also began with a car bomb that breached the complex’s perimeter before gunmen stormed the buildings.

For months, Afghan officials have feared that Taliban fighters displeased with the group’s participation in peace talks would join the Islamic State, potentially bringing battlefield expertise and tactics with them.

Concerns about Taliban involvement in the attack touch on questions regarding the Taliban’s intention to uphold counterterrorism assurances and the ability of the Taliban political leadership in Qatar to control the group’s fighters on the ground. Both issues are becoming increasingly important in the lead-up to peace talks.

Afghan President Ashraf Ghani did not accuse any single group of carrying out the attack, but in a Facebook post Monday, he called it a “barbaric act . . . a crime against humanity and contrary to the Islamic values.”

Until the Sunday night attack, the cease-fire marking the Muslim holiday of Eid al-Adha had largely held. The truce was intended as a confidence-building measure to aid in the push for direct peace talks. After the signing of the U.S.-Taliban peace deal, intra-Afghan negotiations have been delayed for months by an escalation of violence and a controversial prisoner swap.

The Islamic State was not a party to the cease-fire, and Sunday’s attack would probably not constitute a violation of the truce. But it highlights the kind of violence that could continue in Afghanistan even in the event of a peace deal with the Taliban.

Afghanistan’s Islamic State offshoot has asserted responsibility for several recent attacks in Kabul in which gunmen stormed buildings and held off security forces for hours. But the group has largely focused on civilian targets in areas that are home to religious and ethnic minorities, rather than highly fortified government buildings.

The Taliban and Afghan government officials are still wrangling over a controversial prisoner swap that has emerged as a key stumbling block to peace talks.

The U.S.-Taliban peace deal called for the release of prisoners on both sides before the launch of intra-Afghan peace talks. Now, months overdue, the Afghan government announced it would release 5,000 prisoners as the Taliban demanded before talks could begin. But the sides remain at odds over who should be included in the 5,000. Taliban officials seek the release of hundreds of fighters linked to deadly high-profile attacks, a move opposed by government officials and international human rights organizations.

Ghani called for a traditional Afghan consultation, or loya jirga, this week to decide what to do with the disputed Taliban prisoners. If an agreement can be reached, direct peace talks between the Taliban and the Afghan government are expected to begin as soon as later this month.