Gunmen stormed a provincial government complex for agriculture research in the northwestern city of Peshawar on Friday, killing nine students and wounding 35 others, police and rescue officials said.
Police and military troops killed five attackers during a firefight and while clearing the complex, they said.
The main Taliban militant group, Tehrik-e-Taliban Pakistan, claimed responsibility.
Police chief Salahuddin Mahsud of Khyber Pakhtunkhwa province said attackers opened fire on the main gate of a provincial Agricultural Department complex, initially wounding two guards and two students.
Mahsud said three attackers clad in women's burqas then reached the gate in a rickshaw and opened fire to clear their way to the building. Seven others were wounded in the ensuing firefight.
Noor Wali, a 19-year-old student at the institute, described a terrifying ordeal that began when he was woken by gunshots.
"I rushed to the door and saw a fountain of blood pumping out of the shoulder of my roommate who was standing outside the room," he told French Press Agency (AFP).
He and another student rushed to his aid, and hid from the gunmen for almost an hour before they were rescued by security forces.
"The other student was pressing the wound with his hands to stop the blood and I had to put my hand on his mouth so that he cannot scream," he said.
"We took shelter inside the bedroom and were begging the injured student not to make any sound as the terrorists might hear it and kill us."
Mahsud added that dozens of people were rescued.