The prison is believed to hold hundreds of IS members.
The attack highlighted the challenges ahead for Afghanistan, even as US and Nato forces begin to withdraw following a US-Taliban peace deal struck earlier this year.
Afghan Defence Ministry spokesman Fawad Aman said the prison in Jalalabad, the capital of Nangarhar province, located 70 miles east of Kabul, was taken back in the afternoon.
The fighting had also left at least 50 wounded, he said.
Even as Afghan troops retook the prison, IS militants continued to fire on Afghan security forces from a nearby neighbourhood.
Sporadic gunfire rang out from nearby residential buildings in central Jalalabad, an area of high security near the provincial governor’s office.
The 29 dead included civilians, prisoners, guards and Afghan security forces, said Attaullah Khogyani, the provincial governor’s spokesman.
The attack began on Sunday, when an Islamic State suicide bomber drove a car laden with explosives up to the prison’s main gate, detonating the bomb.
Islamic State militants opened fire on the prison’s guards and poured in through the breach.
The IS affiliate in Afghanistan, known as IS in Khorasan province and headquartered in Nangarhar province, later claimed responsibility for the attack.
Some of the 1,500 prisoners there escaped during the fighting.
Mr Khogyani said about 1,000 prisoners who earlier escaped had been found by security forces across the city.
It was not immediately clear if any prisoners were still at large.
The attack came a day after authorities said Afghan special forces killed a senior Islamic State commander near Jalalabad. Several hundred prisoners in Jalalabad are believed to be Islamic State members.
The fighting ended after the final attackers, holed up in the nearby residential complex, were killed, said Mr Aman, the defence ministry’s spokesman.
While the Islamic State group has seen its so-called caliphate stretching across Iraq and Syria eliminated after a years-long campaign, the group has continued fighting in Afghanistan.
The extremists also have battled the Taliban in the country, whom the US overthrew following the 2001 American-led invasion after the September 11 attacks.
The Taliban’s political spokesman, Suhail Shaheen, told The Associated Press that his group was not involved in the Jalalabad attack.
The US struck a peace deal with the Taliban in February. A second, crucial round of negotiations between the Taliban and the political leadership in Kabul has yet to start.
The Taliban had declared a three-day cease-fire starting last Friday for the major Muslim holiday of Eid al-Adha.
The cease-fire expired at 12am Monday, though it was not immediately clear if it would be extended as the US pushes for an early start to intra-Afghan negotiations that have repeatedly been delayed since Washington signed the peace deal with the Taliban.
“We have a cease-fire and are not involved in any of these attacks anywhere in the country,” Mr Shaheen said.
The Taliban also had denied being involved in a suicide bombing in eastern Logar province late on Thursday that killed at least nine people and wounded 40.
Afghanistan has seen a recent spike in violence, with most attacks claimed by the local IS affiliate.