At least 20 people have been killed, including students and a professor, in an attack on a university in northwest Pakistan.

Attackers scaled a wall and cut through barbed wire on Wednesday morning at the Bacha Khan University in Charsadda, about 140km from the capital Islamabad.

A security official said the death toll could rise to as high as 40.

The Pakistani army confirmed that all attackers were killed as the operations to clear the campus ended six hours after the attack began, leaving more than 50 people injured.

"The operation is over and the university has been cleared," Pakistan army spokesperson General Asim Bajwa told Reuters news agency.

"Four gunmen have been killed."

Attackers entered the university buildings in the Khyber-Pakhtunkhwa province of Pakistan and opened fire at students and teachers in classrooms and hostels, according to police officials.

Students told media they saw several young men wielding AK-47 guns storming the university housing many students who were sleeping.

One faction of the Pakistan Taliban, the Dara Adma Khel splinter group, claimed responsibility for the attack.

However, the chief of the Pakistan Taliban, Fazlullah, condemned the attack in a statement, adding that those using the group's name would be brought to justice.

The university has more than 3,000 enrolled students and was hosting an additional 600 visitors on Wednesday for a poetry recital on the anniversary of the death of Pashtun activist Abdul Ghaffar "Bacha" Khan, the university's vice chancellor said.

"Around 200 students have been safely evacuated from an examination hall in the university," Provincial Public Health Engineering Minister Shah Farman said.

Local authorities have announced the closure of all education institutions across Charsadda until January 31.

Earlier, 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 army said the attackers were contained in two university blocks before being killed.

Television footage showed a heavy military presence at the university, troops rushing in and people fleeing.

Ambulances were at the scene and the wounded were being taken to hospital.

Shabir Khan, a lecturer in the English department, said he was about to leave the hostel for the department when the shooting began.

"Most of the students and staff were in the classes when the firing began," Khan said.

On Tuesday, a suicide bomber blew himself up near a police checkpoint in the Jamrud area of northwestern Pakistan, killing at least 10 people and wounding more than 20.

At least 144 people, most of them children, were killed when armed men attacked the Army Public School in Peshawar in December 2014.

Last month, Pakistan executed four men linked to the school massacre which left more than 130 school children dead.

The executions, which officials said were carried out by hanging at a prison in the city of Kohat, were the first in connection with the December 16 attack.