Charsadda (Pakistan) (AFP) - Gunmen armed with grenades and Kalashnikovs stormed a university in northwest Pakistan Wednesday leaving at least 21 dead, in an attack with all the same hallmarks of a 2014 massacre at a nearby Peshawar school.

Security forces killed all four attackers in an assault claimed by a Pakistani Taliban faction but branded "un-Islamic" by the umbrella group's leadership, who vowed to hunt down those responsible.

Security forces said the young attackers had hidden in fields surrounding the Bacha Khan university in the northwestern town of Charsadda, taking advantage of fog that severely restricted visibility to enter the premises unnoticed.

A university security guard told AFP they had scaled a wall to enter the campus, apparently killing a caretaker in a school guesthouse before moving on to the boys' hostel.

Police, soldiers and special forces swarmed the establishment from the ground and the air to shut down the assault, which also left 30 injured.

Pakistan's Prime Minister Nawaz Sharif declared a national day of mourning for Thursday and directed the country's security agencies to hunt those responsible for the attack, his office said.

Wednesday's attack spurred widespread outrage across Pakistan, with a candlelight vigil for the victims held in the southwestern city of Quetta and dozens of people protesting in the port mega-city of Karachi.

Pir Shahab, superintendent of investigations in Charsadda, said the 21 dead included a chemistry lecturer who was later revealed to have died trying to shield his pupils, two gardeners, one caretaker, and 17 students.

The four attackers killed by security forces were not included in the toll, he said.

Pakistan army spokesman Lieutenant General Asim Bajwa told reporters in Peshawar that 18 students and two staff members had been killed, putting the number of deaths at 20.

An AFP reporter saw pools of blood and overturned furniture at a the male students' hostel, where security forces had earlier cornered the four gunmen.

Story continues

Television images earlier showed female students running for their lives and witnesses reported at least two explosions.

- Taliban infighting -

Umar Mansoor, a commander of the Hakimullah Mehsud faction of the Tehreek-e-Taliban Pakistani (TTP), claimed responsibility for the attack, though TTP's central leadership denied any involvement.

"TTP strongly condemns today's attack and disassociates itself completely from this un-Islamic attack," spokesman Muhammad Khurasani said on Twitter, vowing that the group would bring those behind it to justice.

Security forces believe Mansoor was the mastermind behind a similar attack on an army-run school in nearby Peshawar in 2014 that left more than 150 people dead, most of them children.

After a public outcry, the military intensified an offensive in the tribal areas where extremists had previously operated with impunity, and the government launched a crackdown.

The TTP's condemnation appeared to indicate continued infighting in the Pakistani Taliban, as the Islamic State group seeks to recruit its disaffected fighters.

Bajwa said two cell phones were recovered from the scene and had yielded significant information about the attackers, he said.

"They were in contact, one mobile phone was receiving calls even after a terrorist's death," Bajwa said, adding the perpetrators had Afghan simcards.

An anonymous senior security official said the faces of the attackers were recognisable and their fingerprints had been taken, adding: "We hope we will soon identify them."

The official said two of the attackers were teenagers while the others were in their early 20s, and all were heavily armed with Kalashnikovs and grenades.

- Teacher hailed -

Earlier in the day, Pakistanis were hailing Syed Hamid Husain, a chemistry lecturer who tried to protect his students by opening fire on the militants before being shot himself, as a "martyr" and "gentleman".

Witnesses said the professor had been known as "The Protector" even before he died shielding his students.

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

"He would always help the students and he was the one who knew all their secrets... He was referred to by students as 'The Protector'," geology student Waqar Ali told AFP.

Another student told 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.

The gunmen's assault, which Amnesty International said could be branded a war crime, was also condemned globally, including by India, the EU and UN Secretary-General Ban Ki-moon.

"It is particularly appalling that these terrorists continue to attack educational institutions, targeting Pakistan's future generations," said a US State department spokesman.