Police and army troops summoned to the scene killed all the attackers at the Agriculture Training Institute in the northwestern city of Peshawar about two hours into the attack, the military's press wing said.
The Pakistani Taliban claimed responsibility saying in a message from spokesman Mohammad Khorasani that they had targeted a safe house of the military's Inter-Services Intelligence agency.
The gunmen arrived at the campus in an auto-rickshaw, disguised in the burqas worn by many women in the region, Peshawar police chief Tahir Khan said.
A wounded student, Ahteshan ul-Haq, told Reuters the university hostel usually houses nearly 400 students, but most 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.
Shehzad Akbar, medical director of Hayatabad Medical Complex, said six people died of their wounds and 18 were being treated. Another three people died at Khyber Teaching Hospital and 17 wounded were there, director Nekdad Afridi said.
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 U.S.-backed military action in 2001.
(Additional reporting by Asif Shahzad; Writing by Kay Johnson; Editing by Nick Macfie, Robert Birsel)
(Except for the headline, this story has not been edited by NDTV staff and is published from a syndicated feed.)