CHARSADDA, Pakistan -- Islamic militants stormed a university in northwestern Pakistan on Wednesday, killing at least 20 people and triggering an hours-long gunbattle with security forces in an attack that echoed a horrifying assault by the Taliban a little over a year ago on a nearby army-run school.

The attack began shortly after classes started at the Bacha Khan University in Charsadda, a town 21 miles outside Peshawar, said Deputy Commissioner Tahir Zafar. The school may have been targeted because it is named for a late secular icon.

The attackers climbed over the back walls of the university and shot at a security guard before making their way to the administration building and the male students' dorms, police official Saeed Khan Wazir said.

Get Breaking News Delivered to Your Inbox

Soldiers and police raced to the scene and exchanged fire with the attackers. The sound of gunfire and explosions echoed across the campus. Troops later cornered the attackers inside two university blocks, killing all four of them, the army said.

Lt. Gen. Asim Bajwa, the army spokesman, said 18 students and two teachers were killed. One of the teachers included a chemistry professor who was praised as a hero for shooting back at the attackers and allowing some students to escape.

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."

Television footage showed a heavy military presence at the university, with soldiers rushing in and people fleeing. Ambulances raced away, taking the wounded to hospitals.

Botany teacher Mohammad Ishtiaq said he saw five gunmen enter the building he was in and begin firing automatic assault rifles, as students ran in all directions. He said he locked himself inside a second-floor bathroom and then jumped out the window when he saw one of the attackers approaching. He broke his leg in the fall.

Gov. Sardar Mehtab Abbasi later said the attack was over and that troops were combing nearby areas. Pakistani army chief Gen. Raheel Sharif visited the campus, as well as wounded people in a nearby hospital.

The attack revived painful memories of the Taliban assault on an army-run school in December 2014, in which gunmen killed around 150 people, nearly all of them children.

A Taliban leader, Khalifa Umar Mansoor, claimed responsibility for Wednesday's attack in a phone call to The Associated Press from an undisclosed location.

Mansoor, who masterminded the Peshawar school attack, said a four-man Taliban team carried out the latest assault. He said it was in revenge for scores of militants killed by Pakistani security forces in recent months, and that the university was attacked because it was "an instrument of the government and army."

However, a spokesman for the main Taliban faction in Pakistan later disowned the group behind Wednesday's attack, describing the assault as "un-Islamic." Mohammad Khurasani said those who carry out such attacks should be tried before an Islamic court.

The conflicting accounts reflect growing divisions among the various Taliban factions that operate along the porous Pakistan-Afghanistan border, which have been in increasing disarray as U.S. drone strikes have eliminated top commanders in recent years.

The absence of the overall leader of the Pakistani Taliban, Mullah Fazlullah, who is believed to be hiding in Afghanistan, means that local commanders often operate independently.

Khurasani said the Taliban "consider the students in non-military institutions the future of our jihad movement" and would not kill potential recruits. He insisted Mullah Fazlullah had nothing to do with the assault.

Wednesday's assault targeted a school named after Khan Abdul Ghaffar Khan, nicknamed Bacha Khan, a towering secular figure in Pakistani politics who died in 1988. An ally of Mahatma Gandhi, Khan supported non-violent resistance to British colonialism and opposed the 1947 partition.

His son went on to found the Awami National Party, a secular, leftist movement whose vision for Pakistan is starkly opposed to that of the Taliban and other Islamic militants, who for more than a decade have been fighting to overthrow the government and establish a strict Islamic State.

In a statement released after Wednesday's attack, Pakistani Prime Minister Nawaz Sharif said "we are determined and resolved in our commitment to wipe out the menace of terrorism from our homeland."

Pakistan vowed to redouble efforts to combat militants after the Peshawar school attack, lifting a moratorium on the death penalty and intensifying a military offensive in North Waziristan, a tribal region and longtime stronghold of the Taliban and other militants.

Last month, as the country marked the anniversary of the school attack, the military claimed "phenomenal successes" in the war and said it had killed around 3,500 insurgents since launching the operation in 2014.

Militant attacks have declined in Pakistan since the start of the North Waziristan operation. But earlier this week a suicide bomber struck a crowded police checkpoint on the edge of Peshawar, killing 11 people in an attack claimed by the Taliban. Two attacks last week in the southwestern city of Quetta killed 19 people, including soldiers and police.