Four suspected attackers also died in a battle that lasted nearly three hours at Bacha Khan University in Charsadda.
There are conflicting reports about whether Pakistan Taliban militants carried out the assault.
The group killed 130 students at a school in the city of Peshawar, 50km (30 miles) from Charsadda, in 2014.
About 3,000 students are enrolled at Bacha Khan but hundreds of visitors were also expected on Wednesday for a poetry event.
Prime Minister Nawaz Sharif said in a statement, quoted by Reuters news agency: “We are determined and resolved in our commitment to wipe out the menace of terrorism from our homeland.”
“The operation is over and the university has been cleared,” Pakistani army spokesman Gen Asim Bajwa told Reuters news agency. “Four gunmen have been killed.”
The attackers struck at around 09:30 local time (04:30 GMT), apparently climbing over a back wall under cover of the thick winter fog.
Intense gunfire and explosions were heard as security guards fought the attackers.
“I personally heard two explosions,” an unidentified eyewitness told Pakistan’s Geo TV.
“We don’t know if they were suicide bombers or grenades. I personally saw two explosions and smoke was rising.”
Students and staff ran to find cover in toilets and examination halls.
One student told television reporters he was in class when he heard gunshots: “We saw three terrorists shouting, ‘God is great!’ and rushing towards the stairs of our department.
“One student jumped out of the classroom through the window. We never saw him get up.”
Geology student Zahoor Ahmed said his chemistry lecturer, Syed Hamid Husain, had warned him not to leave the building after the first shots were fired.
“He was holding a pistol in his hand,” he was quoted by AFP news agency as saying.
“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.”
The victims – mostly male students – were shot in the head or chest. Seventeen people were injured. At least one security guard also died.
Images from inside the university show a pool of blood on the floor of a dormitory and the charred corpses of two alleged militants lying on a staircase.
Grieving relatives gathered at a hospital as the bodies of victims were placed in coffins.
A senior Taliban commander, Umar Mansoor, told media that the attack was in response to a military offensive against militant strongholds. He said four suicide attackers had carried out the attack.
However, the group’s main spokesman, Mohammad Khurasani, later told the BBC the Taliban had not been involved. He condemned the attack as “un-Islamic”.
An assistant professor at the university, Dr Shakoor, told the BBC he had turned back from the main gate of the campus after being told it was under attack.
Most of the students and members of the faculty would probably still not have arrived when the attack started, he said.
He saw people coming out through the main gate, apparently because the attackers had entered the campus from the back.
The university is located in an open area some distance east of Charsadda town, surrounded by open agricultural fields, and is therefore a soft target, the BBC’s Ilyas Khan reports.
Just days ago, some schools in Peshawar were closed by the authorities amid reports that militants were planning an attack.