Pakistan is observing a day of mourning after a deadly attack by Islamic militants who stormed a northwestern university the day before, gunning down students and teachers and spreading terror before the four gunmen were slain by the military.

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

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

A breakaway Taliban faction took responsibility for the university attack, which raised grim echoes of the 2014 school massacre that left 150 dead, 144 of them children.