The firefight erupted near a training facility overseen by the National Security Directorate - Afghanistan's intelligence agency - with the gunmen holed up in a construction site near residential homes, an official at the scene said.
No group has claimed responsibility, according to local officials.
Afghan police said a suicide bomber walked into the education centre and detonated his explosive belt.
While it has been months since The Taliban have claimed a major attack in Kabul, the group has been conducting blistering attacks on security forces across Afghanistan, including a massive, days-long onslaught on the eastern city of Ghazni during the past week.
IS, which considers Shiites to be heretics, frequently targets them, attacking their mosques, schools and cultural centers.
Their funerals were due to take place on Thursday.
President Ashraf Ghani congratulated Afghanistan's armed forces in Ghazni on Friday for their victory over Taliban insurgents in the strategically important city after clashes that killed at least 150 soldiers and 95 civilians. The remaining victims will be taken to their villages to be buried there, said Gulam Hassan, the cousin of one of the victims.
The public health ministry has put the death toll at 40, and rights group Amnesty International condemned the attack as a "war crime". "Mounting civilian casualties show beyond any doubt that Afghanistan and, in particular, its capital, Kabul, are not safe".
Dawlat Hossain, father of 18-year-old student Fareba who had left her class just a few minutes before the bombing but was still inside the compound, was on his way to meet his daughter and started running when he heard the explosion. "I found her among the unidentified bodies". We are under attack everywhere.
"So we'll continue to see this sort of thing", he said, even though the Taliban lack the strength to hold territory they seize for brief periods. "I won't let anyone stop me, I will resist all terrorist attacks to have the future I want", she said.
Separately on Thursday, Islamic State also claimed responsibility for a suicide bombing in Kabul that tore through an educational centre on Wednesday where teenagers had been studying for a university entrance examination.
The FO spokesman said that Pakistan was fencing the border with Afghanistan with the objective of restricting cross-border movement of both "men and material" as part of its counterterrorism efforts.
Elsewhere in Afghanistan, four policemen were killed and four were seriously wounded late Wednesday when they tried to defuse a auto bomb they found in southern Kandahar province, according to Zia Durrani, provincial police spokesman.
"It took Afghan forces - assisted by USA advisors - five days to repel the insurgents", NPR's Diaa Hadid reports.