Gunmen stormed a university campus book fair, shooting dead at least 19 and wounding more than 20 more.
The hours-long siege at Kabul University came as the Iranian ambassador to Afghanistan was attending the event.
Security forces eventually shot dead all three gunmen.
Throughout the stand-off, grenades and automatic weapons could be heard across the city and witnesses claimed the attackers were deliberately targeting students.
One of the students, Fathullah Moradi, who escaped through the university’s gates with a group of friends, said: “They were shooting at every student they saw.”
Another, Ahmad Samim, said he saw militants armed with pistols and Kalashnikov assault rifles firing at the law and journalism wing of the school, which is the country’s oldest and has around 17,000 students.
The Taliban said their fighters were not involved in the assault and no other group immediately claimed involvement.
The attack came as insurgents continued peace talks with the US-backed government.
The negotiations, taking place in the Gulf Arab state of Qatar, aim to help the US finally withdraw from America’s longest war, though daily bloodshed continues.
At least 24 people including teenage students were killed in a suicide bombing at an education centre in a heavily Shia neighbourhood of western Kabul on 25 October.
Iranian diplomats have been targeted previously by attacks in the country.
In 1998, Iran held the Taliban responsible for the deaths of nine of its diplomats who were working in its consulate in northern Afghanistan.