The Pakistani Taliban has claimed responsibility for an attack on a university in volatile northwestern Pakistan.
Taliban spokesman Umer Mansour told journalists the attack was revenge for the death of militants at the hands of Pakistani security forces.
The claim came as the death toll rose to 19 dead and dozens wounded as the army hunted for any gunmen still holed up on the campus.
A security official said the death toll could rise to as high as 40 as the army cleared out student hostels and classrooms.
A spokesman for the rescue workers, Bilal Ahmad Faizi, said 19 bodies had been recovered including students, guards, policemen and at least one professor.
Firing had ended after several hours and four militants had been killed, the army said, in an attack that comes a little over a year after Taliban gunmen killed 134 students at a military-run school in nearby Peshawar.
A senior security officer at the scene told Reuters 90 percent of the campus had been secured and that 51 people were wounded.
The militants, using the cover of thick, wintry fog, scaled the walls of the Bacha Khan University in Charsadda, northwestern Khyber Pakhtunkhwa province, before entering buildings and opening fire on students and teachers in classrooms and hostels, police said.
The gunmen attacked as the university prepared to host a poetry recital on Wednesday afternoon (local time) to commemorate the death anniversary of Khan Abdul Ghaffar Khan, a popular ethnic Pashtun independence activist after whom the university is named.
Police inspector Saeed Wazir said 70 percent of the students had been rescued.
"All students have been evacuated from the hostels, but militants are still hiding in different parts of the university and some students and staff are stuck inside," he said before the firing had stopped, adding that it was unclear how many gunmen were involved.
Television footage showed soldiers entering the campus as ambulances lined up outside the main gate and anxious parents consoled each other.
Shabir Khan, a lecturer in the English department, said he was about to leave his university housing for the department when firing began.
"Most of the students and staff were in classes when the firing began," Khan said. "I have no idea about what's going on but I heard one security official talking on the phone to someone and said many people had been killed and injured."
Pakistan, which has suffered from years of jihadist militant violence, has killed and arrested hundreds of suspected militants under a major crackdown launched after the massacre of school children in December 2014 in Peshawar.
The school attack by six Pakistani Taliban gunmen hit a raw nerve in Pakistan and was seen as having hardened Pakistan's resolve to fight militants along its lawless border with Afghanistan.
"We are determined and resolved in our commitment to wipe out the menace of terrorism from our homeland," Pakistani Prime Minister Nawaz Sharif said in a statement after Wednesday's attack.