Gunmen stormed Kabul University, killing students in their classrooms and firing on others as they fled on Monday, in the second attack on an educational institution in the city in just over a week, officials and witnesses said.
The three attackers killed at least 19 people including students before Afghan security forces shot the gunmen dead, authorities said.
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 they were climbing out of a window, reports Reuters.
"They were shooting at every student they saw ... they even shot at the students who were running away," witness Fathullah Moradi told Reuters.
The Taliban said their fighters were not involved in the assault. No other group immediately claimed responsibility.
Nineteen people were killed and 22 wounded before the security services ended the assault, interior ministry spokesman Tariq Arian said.
"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 claimed responsibility for that attack, without providing evidence.
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.