"Cowards and their finances will see our national resolve to eliminate terror," his office said. He said the nation was united.
Shaukat Yousafzai, a local lawmaker, said in an interview based on preliminary information at least 20 had been killed. Yousafzai said at least 50 people had been injured, many of whom are suffering from gunshot wounds. Some Pakistani media outlets were reporting that the death toll could rise considerably.
In a statement, a spokesman for the Pakistan Taliban claimed credit for the attack, saying it was carried out by four militants. Major General Asim Bajwa, a spokesman for the Pakistan military, said security forces that converged on the campus have so far killed four suspected terrorists. But Bajwa said the operation was ongoing.
The co-ed university is named after Bacha Khan, a Pashtun nationalist leader who was the founder of Pakistan's Awami National Party.
The party is known for its strong anti-Taliban views, and many of its leaders have been killed in recent years. Wednesday was the 28th anniversary of Khan's death. The attack occurred as a gathering of Pashtun poets was taking place on campus to commemorate the anniversary.
Saeed Khan Wazir, a senior police officer, told media that the gunmen snuck on to school grounds using the cover of Pakistan's chronically foggy mornings during the winter.
"There was severe fog and visibility was almost none," Wazir said.
One student told Pakistan's Channel 24 news that he was in his dormitory when he heard gunshots.
"It was a deafening sound, and first we decided to go out and run but upon hearing continuous firing, we shut our room doors," the student said. "Two terrorists came to my door and shouted, 'We are army and we are here to rescue you.' But I didn't open the door.
"After this, they started firing at the door but I lied down on the floor silently waiting till they were gone."
Prime Minister Sharif, who is in Zurich for a global economic conference, said earlier that law enforcement agencies converged on the scene to rescue students and faculty members.
"We are determined and resolved in our commitment to wipe out the menace of terrorism from our homeland," he said. "The countless sacrifices made by our countrymen will not go in vain."
In December 2014, a terrorist attack at an army-run school in Peshawar killed about 150 teachers and students. After that, Pakistani officials greatly enhanced security at educational establishments, including erecting walls lined with razor wire and mandating the presence of armed guards at some institutions. Some provinces in Pakistan even authorised teachers to carry firearms in the classroom.
But there have been repeated warnings that schools remained vulnerable to attack. On Tuesday, parents throughout northwestern Pakistan rushed to pull their children out of school after rumours spread through communities that a terrorist attack on a school may be imminent.
The Pakistani Taliban is an offshoot of the Taliban insurgency in Afghanistan. In Pakistan, the group is pushing for the imposition of Sharia law. Since its founding in the mid-2000s, more than 50,000 Pakistanis have been killed in terrorist attacks or battles between the military and Islamist militants.
In June 2014, after an attack on Karachi's international airport killed more than two-dozen people, the Pakistani military launched a major operation to drive Islamist militants from their safe-havens in northwestern Pakistan's tribal belt. The operation intensified a year ago after the Peshawar school attack.
Throughout 2015, there had been a marked decline in violence in Pakistan. According to a recent report by the Pak Institute of Peace Studies, 2015 was Pakistan's safest year since 2007 because terrorist attacks dropped by 48 per cent compared with the previous year.
"The spaces for extremist's apologists in public discussions and mainstream media are gradually shrinking, which contributed in keeping the discourse on counterterrorism focused," the report concluded.
Security and analysts, however, have repeatedly stressed that the Pakistani Taliban was still capable of pulling off headline-grabbing attacks, especially in the northwestern part of the country. In September, the Pakistani Taliban took credit for an attack on a Pakistani Air Force base in Peshawar, killing 29 people.
The Pakistani Taliban, whose leadership is believed to reside in Afghanistan, also claimed credit for setting a roadside bomb that killed six people near a military checkpoint in Khyber Agency on Tuesday. A day earlier, five Pakistani soldiers were killed by a roadside bomb in the western city of Quetta.