Afghan forces say they have retaken prison after at least 29 were killed in hours-long battle following ISIL-claimed attack on the facility.