A spokesman for the Afghan interior ministry said the attack was eventually stopped when three gunmen were killed.
A regional Islamic State group claimed responsibility in a statement.
The attack began shortly before the expected arrival of government officials for an Iranian book fair and continued for several hours.
A further 22 people were wounded by the attackers.
Tuesday has been declared a national day of mourning by the government.
The Taliban denied involvement and condemned the attack shortly after it began on Monday. Hours later the Islamic State group issued a message on the Telegram app saying it had targeted “the graduation of judges and investigators working for the apostate Afghan government”.
IS has previously targeted education centres in Afghanistan, including an attack outside a tuition centre in Kabul last month that left 24 people dead. The group also claimed responsibility for a 2018 attack in front of Kabul University in which dozens were killed.
In a statement issued by the presidential palace, President Ashraf Ghani said authorities would “take revenge for this senseless attack”.
Video footage from the university campus on Monday showed students running away from the site with the sound of gunfire in the background. Some scaled walls in an effort to escape. One of the attackers detonated explosives at the beginning of the assault, according to a spokesman for the Interior Ministry.
Fraidoon Ahmadi, a 23-year-old student, was in class when the gunfire started: “We were very scared and we thought it could be the last day of our lives. Boys and girls were shouting, praying and crying for help,” he told the AFP news agency.
Another witness, Fathullah Moradi, told Reuters the gunmen were “shooting at every student they saw” and “even shot at the students who were running away”.
Violence in Afghanistan has worsened in recent months even as the Taliban conducts peace talks with the government in Doha, Qatar. The talks, which follow an earlier peace agreement between the Taliban and the US in February, have stalled over preliminary issues.
A senior UN official told the BBC last week that al-Qaeda was still “heavily embedded” within the Taliban , despite assurances from Taliban officials to the US that it would sever ties with the terror group.
Separately on Monday, a vehicle hit a roadside mine in the country’s southern Helmand province, killing at least seven people, most of them women and children, according to the provincial governor’s spokesman Omer Zwak.
Courtesy – bbc.co.uk