CHARSADDA, Pakistan -- Once again, Islamic militants stormed a school in northeastern Pakistan in a deadly attack that lasted for hours. And once again, the blood of students and teachers stained classrooms and hallways, raising questions about whether security forces are able to protect the country's educational institutions from extremists.

At least 20 people were killed and 23 were wounded Wednesday in the assault at Bacha Khan University in Charsadda before the four gunmen were slain and the military declared an end to the siege. Two teachers were among the dead, including a chemistry professor who was praised as a hero for shooting back at the attackers and allowing some students to escape.

The university attack was grimly reminiscent of the December 2014 massacre at an army public school in nearby Peshawar that killed 150, mostly children.

A breakaway faction of the Taliban took responsibility for the university attack, although a spokesman for the larger Taliban organization, led by Mullah Fazlullah, denied having anything to do with it and called it "un-Islamic."

The violence shows how vulnerable schools remain in Pakistan, where extremists have sought to prevent Western-style education, especially for girls.

Malala Yousafzai was awarded the Nobel Peace Prize after the teenager was shot in the head by a Taliban gunman in 2012 outside her school in the Swat Valley because of her vocal support for gender equality and education for girls. She said she was "heartbroken" by the latest attack.

Several schools were closed last weekend after intelligence suggested militants were planning an attack, according to Muhammed Amir Rana, director of the private Pakistan Institute for Peace Studies. A provincial government spokesman said they were closed as part of a security drill.

After the Peshawar attack, the government promised to set up a joint Intelligence Directorate, but that has not happened yet.

The military is one of Pakistan's most powerful institutions, as is the intelligence agency, known as the ISI. It is especially difficult for civilian governments to penetrate that authority and establish intelligence sharing with government-operated security forces such as the police.

"The government is trying to develop a response but is facing capacity issues," Rana said, particularly in the area of intelligence-sharing among the powerful intelligence agencies and the police.

The army has been pounding militant hideouts in the tribal areas on the border with Afghanistan since June 2014, disrupting operations for the Pakistani Taliban militants. Because of that campaign, analysts say the extremists have turned to attacking soft targets such as schools.

"We are determined and resolved in our commitment to wipe out the menace of terrorism from our homeland," Prime Minister Nawaz Sharif said in a statement after the attack.

A breakaway Taliban faction led by Khalifa Umar Mansoor said it had carried out the attack.

But a statement emailed to news organizations by Muhammad Khorasani, the spokesman for the Tehrik-e-Taliban, the largest Taliban group, said: "We disown, condemn the attack and term it as un-Islamic."

After the 2014 Peshawar school massacre, Taliban militants were united in taking responsibility for the violence.

Rana, whose institute tracks militant movement, said the divisions in the Taliban over who carried out Wednesday's attack probably has more to do with a fear of retribution than a reflection of a deeply divided Taliban.

The backlash that followed the Peshawar attack was so severe that it probably left the Taliban "reluctant to take credit, he added, noting that Afghan security forces joined in operations against Pakistani Taliban hideouts afterward.

The four militants invaded the campus of Bacha Khan University shortly after classes began for the day in Charsadda, about 35 kilometres (21 miles) outside Peshawar, said Deputy Commissioner Tahir Zafar. Both men and women attend the school, which has about 3,000 students, said its vice chancellor, Fazle ur-Rahim Marwat.

The university is named for one of Pakistan's greatest secular leaders, Abdul Ghaffar Khan, known as Bacha Khan, who often espoused communist philosophy. The attack coincided with the 28th anniversary of Bacha Khan's death on Jan. 20, 1988.

As police and soldiers rushed to the scene, the attackers traded gunfire with the troops, and several explosions were heard. The attackers were later contained inside two university blocks where the troops killed them, the army said.

Among the 18 students and two teachers who were slain was Syed Hamid Hussain, a chemistry professor who witnesses said opened fire on the gunmen. Hussain fired as he moved backward, herding his class out behind him before being killed in the gunbattle, said student Bilal Khan.

The attackers carried mobile phones with Afghan numbers and "were in touch with their handlers in Afghanistan," said Pakistan military spokesman Lt. Gen Asim Bajwa. Pakistan maintains that its militants often find refuge in Afghanistan.

He told a news conference in Peshawar that the militants hate education because it is a symbol of progress.

In a statement on the Malala Fund's social media site, Yousafzai said: "This brutality must be stopped."

"I am heartbroken by the attack on students and staff at the Bacha Khan University in Charsadda and strongly condemn this brutal assault," she said. "My prayers are with the families of all the victims and all those who suffer as a result of extremist violence."

She also called for Pakistani authorities to ensure "that all schools and universities are safe. I urge all people with peace in their hearts to renew their resolve to stand up to terrorism and ignorance, and work together to protect life and learning."

UN Secretary-General Ban Ki-moon, who also condemned the attack, reaffirmed that violence against students, teachers and schools can never be justified, and that "the right to education for all must be firmly protected," said UN deputy spokesman Farhan Haq.

Ban called for those behind the attack be brought to justice, adding that there must be "proportionate and necessary measures to be taken to ensure that schools in areas of insecurity and conflict are adequately protected," Haq said.

Abdul Ghani, whose son fled the carnage by running into a nearby sugar cane field, decried that Pakistani students are "at risk of being killed."

Marwat, the vice chancellor, said security forces alone could not keep students safe, saying it required a move away from an extreme interpretation of Islam.

Since the start of the new year, Pakistan has been battered by five militant attacks, most of them targeting security forces. Such attacks decreased last year, with the military saying it made great strides in crushing the violence.