A senior Pakistani Taliban commander initially claimed responsibility for the attack, but this was later contradicted by the official Pakistani Taliban spokesman Muhammad Khorasani, who denied involvement. The reasons for the conflicting statements are not clear.
The gunmen used the cover of thick fog to climb the walls of the Bacha Khan University before entering classrooms and hostels and beginning their attack.
Security officials warned the death toll could rise as the army worked its way through student hostels and classrooms. Among the dead were students, guards, policemen and at least one professor. Four militants were also killed.
Police and troops rushed to the scene and a violent gun battle followed. Shooting continued for several hours.
A senior security officer at the scene said 51 people had been wounded.
Students targeted
Just over a year earlier Taliban gunmen attacked a military-run school in nearby Peshawar, killing 141 people, including 134 schoolchildren aged from eight to 18.
The target of the latest attack, a university in the north-western Khyber Pakhtunkhwa province, is named after the founder of a liberal anti-Taliban political party.
Several other schools around Peshawar, the capital of the province, had closed early at the weekend after rumours circulated about a possible attack.