Taliban gunmen stormed a university in northwestern Pakistan on Wednesday, killing at least 20 people and triggering an hours-long gunbattle with the army and police before the military declared the assault in a town near the city of Peshawar was over.

The attack stirred grim echoes of the horrific 2014 Peshawar school attack that killed more than 150 people, mostly children, and shocked the nation. It also prompted the Pakistani prime minister to pledge the country will wipe out the 'menace of terrorism.' Police said four attackers were also killed.

Wednesday's attack began shortly after the Bacha Khan University opened for classes in the town of Charsadda, some 35 kilometres outside Peshawar, said Deputy Commissioner Tahir Zafar.

As police and soldiers rushed to the campus, the attackers traded gunfire with the troops and several explosions were heard from the area of the university. The attackers were later contained inside two university blocks where the troops killed four attackers, the army said.

A chemistry professor and a student were among those killed, said Zafar, adding that it was not initially clear how many attackers managed to penetrate the campus. Television footage showed heavy military presence at the university, troops rushing in and people fleeing. Ambulances were at the scene and the wounded were taken to hospital.

Charsadda

This photograph taken from a mobile phone shows Pakistani security personnel taking position Wednesday outside the Bacha Khan university following an attack by gunmen in Charsadda, about 50 kilomere from Peshawar. (A Majeed/AFP/Getty Images)

The attackers entered the university compound by climbing over back walls and shooting at a security guard before they made their way to the administration building and the male students' dorms, police official Saeed Khan Wazir said.

A witness, botany teacher Mohammad Ishtiaq, said he jumped out from the second floor of the building when he heard gunshots and broke his leg. Two attackers were on first floor and three on the ground floor, he said, adding that they were using automatic assault rifles. The students ran in different directions, he said.

"I locked myself in a washroom," he said. "I jumped out when I saw one of the attackers coming toward me and shooting straight ahead of him."

Pakistani Prime Minister Nawaz Sharif vowed to fight to the end and destroy the Taliban and other militants.

"We are determined and resolved in our commitment to wipe out the menace of terrorism from our homeland," Sharif said in a statement.

Claim of responsibility

A Taliban leader, Khalifa Umar Mansoor, claimed responsibility for Wednesday's attack in a phone call to The Associated Press from an undisclosed location.

Mansoor, who was the mastermind of the Peshawar school attack, said a four-man Taliban team carried out the assault in Chasadda. He said it was in revenge for the scores of militants the Pakistani security forces have killed in recent months.

However, a spokesman for the main Taliban faction in Pakistan later disowned the group behind Wednesday's attack, describing the assault as "un-Islamic." Mohammad Khurasani also denied earlier reports that he had endorsed Mansoor's claim and said that those who carried out such attacks would be tried before an Islamic, or Sharia court.

Such statements from among the Taliban are not uncommon since the group has many loosely linked factions and is indicative of the deep divisions and splits among the insurgents.

Khurasani also said the Taliban "consider the students in non-military institutions the future of our jihad movement" and would not kill potential future followers. He insisted Mullah Fazlullah, the head of the Pakistani Taliban, had nothing to do with the assault.

Attack follows suicide bombing

Wednesday's attack was the second major attack in as many days in the volatile northwest. A suicide bomber riding a motorcycle struck a crowded police checkpoint on the outskirts of Peshawar on Tuesday, killing 11 people in an attack that was also claimed by the Taliban.

Later in the afternoon, provincial governor Sardar Mehtab Abbasi declared that the military operation on the campus grounds had been completed and that the attack was over. The troops were combing the nearby areas, searching for more possible attackers, he said.

Pakistani army chief Gen. Raheel Sharif visited the campus and a town hospital where the wounded were brought to.

The Bacha Khan University is named after the founder of a liberal, anti-Taliban political party. The Pakistani Taliban have in the past targeted the party for its anti-militant policies.