The number of dead climbed rapidly after armed men stormed the Bacha Khan university in Charsadda, about 50 kilometres (30 miles) from the city of Peshawar, in the latest outrage to hit the militant-infested region.
Police, soldiers and special forces swarmed the university from the ground and the air in a bid to shut down the assault, as television images showed female students running for their lives.
"The death toll in the terrorist attack has risen to 21," regional police chief Saeed Wazir told AFP hours after the alarm was first raised.
He said the operation had ended and security forces were clearing the area, with most of the student victims shot dead at a hostel for male students.
"More than 30 others including students, staff and security guards were wounded," he added.
Emergency official Bilal Faizi described seeing five bodies, all with bullet wounds.
Military spokesman Major General Asim Bajwa said on Twitter that four attackers had been killed.
"Update: Snipers killed 2 more terrorists on roof top, total Terrorist killed so far 4. All buildings (and) roof top taken over by Army. op continues," he wrote earlier.
It was not immediately clear if the four were included in the toll of 21 given by police chief Wazir.
The Pakistani Taliban claimed responsibility for the attack.
"Our four suicide attackers carried out the attack on Bacha Khan University today," Umar Mansoor, a commander in the Tehreek-e-Taliban Pakistani (TTP) militant group told AFP.
Speaking by telephone from an undisclosed location, he said it was in response to a military offensive against extremists in the tribal areas.
Teacher fighting back
Students spoke of one hero teacher -- named by media as Syed Hamid Hussain -- fighting back against the intruders, shooting his weapon in a bid to protect his charges.
Geology student Zahoor Ahmed said his chemistry lecturer had warned him not to leave the building after the first shots were fired.
"He was holding a pistol in his hand," he said.
"Then I saw a bullet hit him. I saw two militants were firing. I ran inside and then managed to flee by jumping over the back wall."
Another student told television reporters he was in class when he heard gunshots.
"We saw three terrorists shouting, 'Allah is great!' and rushing towards the stairs of our department," he said.
"One student jumped out of the classroom through the window. We never saw him get up."
He also described seeing the chemistry professor holding a pistol and firing at the attackers.
"Then we saw him fall down and as the terrorists entered the (registrar) office we ran away."
Pakistan's President Mamnoon Hussain confirmed the lecturer, Dr Hamid, had died.
Witnesses said dozens of personnel in combat fatigues and carrying automatic weapons had descended on the campus after the attack began, as helicopters buzzed overhead and ambulances raced to the scene.
Officials at hospitals in the city confirmed they were receiving injured patients, and Shaukat Yousafzai, provincial information minister told media that some of the injured were being taken to Peshawar.
Security 'beefed up'
The attack had echoes of a Taliban assault on an army-run school in Peshawar in December 2014 that killed more than 150 people, most of them children.
![Pakistani rescuers shift an injured man to a hospital following an attack by gunmen in the Bacha Khan university in Charsadda](http://img.astroawani.com/2016-01/71453279084_freesize.jpg)
"There are male and female staff members and students on the campus," university vice chancellor Fazal Raheem Marwat said, adding he had been on his way to work when he was informed of the attack.
"There was no announced threat but we had already beefed up security at the university."
Naik Mohammed, security chief at the university, said the attackers had entered close to a campus guest house.
The 2014 assault in Peshawar was Pakistan's deadliest ever attack, and prompted a crackdown on extremism in the country.
After a public outcry, the military intensified an offensive in the tribal areas where extremists had previously operated with impunity.
Pakistan's Jinnah Institute said in a report released Tuesday that the National Action Plan (NAP) helped curb extremist violence last year, although targeted attacks against religious minorities spiked in the Muslim nation of some 200 million people.
On Tuesday, a suicide attack at a market on the city's outskirts killed 10 people, in addition to the bomber.