Pakistanis buried their dead and observed a day of nationwide mourning Thursday following the brazen attack by Islamic militants who stormed a northwestern university the previous day, gunning down students and teachers and spreading terror before the four gunmen were slain by the military.

Meanwhile, the death toll from the assault at Bacha Khan university in the town of Charsadda rose to 21, after another student died in hospital, said police official Tariq Khan.

Most of the victims were students and their families were inconsolable.

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.

"My son was grown up, but still he was an innocent kid for me," said Gula Bibi, the mother of the second slain teacher, Iftikhar Ahmad, who was also the university librarian.

PAKISTAN-ATTACKS/UNIVERSITY

A girl prays for the victims of a militant attack on the Bacha Khan University, during a candle light vigil in Peshawar late Wednesday. (Khuram Parvez/Reuters)

"My heart is breaking apart, I don't know what to do," she said.

The attack, which also wounded 22 students, raised grim echoes of the 2014 school massacre in the nearby city of Peshawar that left 150 dead, 144 of them children. It yet again raised questions about whether security forces are able to protect the country's educational institutions from extremists.

Flags on official buildings and the parliament were flying at half-staff and police stepped up security at schools and educational centres across the country.

A breakaway Taliban faction claimed responsibility for the assault — the same faction, headed by Khalifa Umar Mansoor, which had claimed the Peshawar school assault.

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

Most of the victims were buried quickly, according to Muslim tradition, with funerals overnight and early Thursday, said Khan, the police official.

Mohammad Khurasani. A spokesman for the main Taliban group in Pakistan disowned those behind the university attack, terming it "un-Islamic" and insisted the Pakistani Taliban were not behind it.

Wednesday's violence yet again exposed the vulnerability of schools 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.

There was tight security at all Pakistani schools and educational institutions Thursday, with schoolbags scanned and teachers and students checked before being allowed in.

The Bacha Khan university remained closed and its vice chancellor Fazal-ur-Rahim Marwat said classes would resume Monday.

"We need time to clean the campus, make more security arrangement and boost the morale of the students and teachers," he said.

Cricket legend-turned-politician Imran Khan, who heads the party that rules the Khyber Pakhtunkhwa province where Charsadda is located, said it was impossible to provide police guards at every school or educational centre.

There're around 64,000 educational institutions in the province alone, he said, and defended measures already taken. The Bacha Khan university had 54 guards, he added — and still the attack happened.

"There were intelligence reports of a threat to schools some days ago," he said. "We sent kids home that day. We took all possible measures."