Police and army troops summoned to the scene killed all of the attackers at the Directorate of Agriculture Institute in the northwestern city of Peshawar about two hours into the attack, the military's press wing said.
The Pakistani Taliban claimed responsibility for the attack, saying in message from spokesman Mohammad Khorasani that they had targeted a safe house of the military's Inter-Services Intelligence (ISI) agency.
The gunmen arrived at the campus in an auto-rickshaw and disguised in the burkas worn by many women in the region, Peshawar police chief Tahir Khan said.
They shot and wounded a guard before entering the campus, he said, adding that five wounded people had been taken to hospital.
A wounded student, Ahteshan ul-Haq, told Reuters that the university hostel usually houses nearly 400 students, but most of them had gone home for a long holiday weekend and only about 120 students remained.
“We were sleeping when we heard gunshots. I got up and within seconds everybody was running and shouting 'the Taliban have attacked',” he said.
In December 2014, Pakistani Taliban gunmen killed 134 children at Peshawar's Army Public School, one of the single deadliest attacks in the country's history.
The Pakistani Taliban are fighting to topple the government and install a strict interpretation of Islamic law. They are loosely allied with the Afghan Taliban insurgents who ruled most of Afghanistan until they were overthrown by US-backed military action in 2001.
Join our new commenting forum
Join thought-provoking conversations, follow other Independent readers and see their replies