Assistant professor Syed Hamid Hussain, 34, fought back against the terrorists as he warned students not to leave the building of the Bacha Khan university in Charsadda, some 50 kms southwest of Peshawar.
Students spoke of the hero teacher, who flashed his pistol but was gunned down by the militants besides 24 others, media reports said.
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 slogans and rushing towards the stairs of our department," he said as he 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," said the student.
President Mamnoon Hussain confirmed Hamid was among the deceased and condoled his death.
Netizens took to social networking sites like Twitter to hail the lecturer as a "martyr".
"Martyr of #education: Prof Hamid who was killed by terrorists in #BachaKhanUniversity #Pakistan," tweeted journalist and academic Raza Ahmad Rumi.
The official Facebook page of the university also said Hamid was among those killed.
Teachers in northwest Pakistan were given permission to carry firearms in the classroom after the Peshawar school carnage of December 2014 in which Taliban militants killed over 150 people, mostly children.
Image: Syed Hamid Hussain, 34, died fighting against the terrorists. Photograph: Bacha Khan University/ www.bkuc.edu.pk
Pak test-fires nuclear-capable Shaheen-III ballistic missile
Pakistan hangs 4 militants linked to Peshawar school attack
The most dangerous terrorist in Pakistan
6 most horrifying terror attacks on schools