Gunmen stormed Kabul University, killing at least 22 people including students in their classrooms, on Monday and Islamic State militants claimed responsibility for what President Ashraf Ghani called a "despicable act of terror".
In a video message, Ghani, who once lectured at the university, announced a national day of mourning to honour the victims and offered his "condolences and profound sympathies to the nation" and the families of the victims.
"My heart is still beating for this academic institution," he said. "Today's attack has left us grief-stricken."
Gunmen barged into Kabul University in the morning, killing students in their classrooms and firing on others as they fled, officials and witnesses said, in what was the second attack on an educational institution in the capital in just over a week.
The three attackers killed at least 22 people, including students, and wounded 22 others before Afghan security forces shot the gunmen dead, the health ministry said.
The attack was claimed by Islamic State group, the jihadist group's Amaq News Agency said. Amaq said the gunmen targeted a gathering being held to mark the completion of a training course at the university.
Photos shared by a senior government official showed students lying dead in classrooms, some next to their books. One student appeared to have been shot as he was climbing out of a window.
"They were shooting at every student they saw...They even shot at the students who were running away," witness Fathullah Moradi told Reuters.
"This is the second attack on educational institutions in Kabul ... Afghan children & youth need to feel safe going to school," NATO Senior Civilian Representative to Afghanistan Stefano Pontecorvo said in a statement.
A suicide bomber killed 24 people including teenage students at an education centre in Kabul on Oct. 24. Islamic State group claimed responsibility for that attack, without providing evidence.
Taliban insurgents issued a statement condemning the latest attack and denying any involvement.
Violence has plagued Afghanistan while government and Taliban negotiators have been meeting in Qatar to try to broker a peace deal and as the United States brings home its troops.
In a post on Twitter, the Presidential Palace announced a day of mourning on Tuesday, for which the Afghan flag will fly at half mast in the country and at its diplomatic missions around the world.