A chemistry teacher who tried to shield his students by opening fire on Taliban militants during a deadly attack at a Pakistani university was known as "The Protector" even before his death in a hail of bullets Wednesday.

Lecturer Syed Hamid Husain, a 32-year-old assistant professor of chemistry at the Bacha Khan university in Charsadda, ordered his students to stay inside as Taliban gunmen stormed the school near the city of Peshawar on Wednesday, leaving at least 21 people dead.

Students told of how the father-of-two opened fire on assailants as they rampaged across campus, giving the young people time to flee before he was cut down by gunfire.

"We saw three terrorists shouting, 'Allah is great!' and rushing towards the stairs of our department," one man told reporters.

"One student jumped out of the classroom through the window. We never saw him get up."

He described seeing Husain 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."

Geology student Zahoor Ahmed said Husain 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."

"They fired directly at" the professor, sociology student Muhammad Daud told AFP, describing Husain as "a real gentleman and a respectable teacher".

Students and university officials paid tribute to the slain academic Wednesday, saying he had been nicknamed "The Protector" even before his death.

"He would always help the students and he was the one who knew all their secrets because they would share all their problems with him," 22-year-old geology student Waqar Ali told AFP.

"He was referred to by students as 'The Protector'."

- 'Remember, kiddo, I have a pistol' -

Husain had been the father of a three-year-old boy and a daughter who had recently celebrated her first birthday, a university administration official told AFP.

Story continues

He had spent three years studying in the UK for his PhD, the official said.

Mohammad Shazeb, a 24-year-old computer science student, told AFP that Husain was fond of gardening and used to joke with the students that they should learn gardening for when they are unemployed.

"He had a 9mm pistol and used to tell us stories about his hunting trips," Shazeb said.

Husain also never missed a game of cricket with the students, he said, adding: "When someone would go to bowl to him, he would joke: 'Remember kiddo, I have a pistol'".

Tributes were also paid online to the slain teacher, whose funeral was held in his home village of Swabi Wednesday evening.

"Martyr of #education: Prof Hamid who was killed by terrorists in #BachaKhanUniversity#Pakistan," tweeted journalist and academic Raza Ahmad Rumi.

Pakistan's President Mamnoon Hussain expressed his grief and condolences to the man's family.

Police said at least 21 people had been killed in the university attack, with security forces killing all four gunmen. It was not clear if they were included in the toll.

A faction of the Pakistani Taliban have claimed responsibility for the attack, though the umbrella group's main leadership has condemned it as "un-Islamic".

Teachers in northwest Pakistan were given permission to carry firearms in the classroom after Taliban militants massacred more than 150 people, the majority of them children, at a school in the city of Peshawar in 2014.

The attack on an army-run school in the city, some 50 kilometres (30 miles) from Charsadda, was the deadliest in Pakistani history, and saw heavily armed militants go from room to room slaughtering students and staff.

Teachers' associations had objected to arming staff, saying it was not their job to fight off militants.