At least 21 people died in an armed assault on a university in Pakistan Wednesday, where witnesses reported two large explosions as security forces moved in under dense fog to halt the bloodshed.

The number of dead rose rapidly after armed men stormed the Bacha Khan university in Charsadda, about 50 kilometres 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 boys on the campus.

"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 12 given by police chief Wazir.

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."

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.

TALIBAN

No group immediately claimed responsibility for the attack, which 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.

"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 Taliban assault on the Peshawar school was Pakistan's deadliest ever attack, and prompted a crackdown on extremism in Pakistan.

After a public outcry, the military launched an offensive against extremists in the tribal areas where they 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.

"The NAP has allowed improvements in two areas: the first actual implementation of prosecution against hate speech, and the arrest of terrorists from sectarian organisations which feed religious violence," said one of the authors, Syed Hassan Akbar.

On Tuesday, a suicide attack at a market on the city's outskirts killed 10 people, in addition to the bomber.