The militants attacked the student hostel of the Directorate of Agriculture Extension on the University Road in the city, the capital of the restive Khyber-Pakhtunkhwa province.
Security officials said that militants clad in burqa used an auto rickshaw to reach the building.
They stormed the building by firing automatic weapons, creating panic, officials said.
The Tehreek-i-Taliban Pakistan claimed responsibility for the attack.
Hospital authorities said nine persons mostly students were killed in the attack. Thirty-two people were also injured in the attack.
A senior police official said all the terrorists were killed by security forces.