The attack happened at the University of Peshawar where police had taken the detainees from the city's central jail.
"They were taken to hospital for a dental check-up. The gunmen fired at them when they returned from the hospital," Peshawar police Chief Imtiaz Altaf said.
"The prisoners were arrested under terrorism charges. The gunmen came on two motorcycles and a car, and escaped with them after killing three policemen and injuring one," said police official Mohammed Ijaz.
Security officials told AFP on condition of anonymity that eight to 10 gunmen had been lying in wait outside the dental college.
Copyright AFP (Agence France-Presse), 2011