Nearly three hours after the attack began, an army official said firing had stopped but troops were searching Bacha Khan University campus in Charsadda.
The army said four attackers had been killed as the Taliban reported four of its suicide attackers were involved.
The Taliban killed 130 students at a school in nearby Peshawar in 2014.
Charsadda is about 50km (30 miles) from the city.
Wednesday's attackers struck at around 09:30 local time (04:30 GMT), reportedly climbing over the back wall, where a university guesthouse is located.
Intense gunfire and explosions were heard as security guards fought the attackers.
"I personally heard two explosions near hostel number one," 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.
By the time police and Pakistani army commandos had arrived, several students and staff members were dead and dozens of others wounded.
Most of the people on campus have now been evacuated.
Nineteen bodies were taken to a local mortuary.
A Taliban spokesman told AFP news agency the attack was in response to a military offensive against militant strongholds.
(BBC News)