Daesh claimed responsibility for the attack that comes weeks after the Iraqi government declared victory over the group in Mosul following a gruelling fight that lasted for more than eight months.
Two Afghan guards were killed in a suicide and gun attack on the Iraqi embassy in Afghanistan's capital Kabul on Monday, the Iraqi foreign ministry said.
Iraq's chargé d'affaires in the Afghan capital was rescued from the premises of the targeted embassy and whisked away to the Egyptian embassy, the Iraqi foreign ministry in Baghdad added.
A suicide bomber blew himself up outside the embassy and militants breached the compound, Afghan officials said, in an attack claimed by Daesh. The assailants took cover in the embassy building in a business district of the city, the officials further said.
Black smoke billowed into the air above the neighbourhood in northwestern Kabul as the sound of ambulance sirens could be heard.
The assault ended roughly four hours after the militants launched their assault.
TRT World's Ben Said has this report.
"The terrorist attack on the Iraqi embassy is over. All the attackers have been killed," the Afghan interior ministry said in a statement, adding that no embassy staff had been harmed.
The interior ministry earlier confirmed that at least four militants had attacked the Iraqi embassy, saying that a suicide bomber had detonated his vest at the compound entrance.
"Police reacted quickly and the embassy staff are shifted to a safe location," the statement continued.
Two members of "the Islamic State [Daesh] attacked the Iraqi embassy building in the Afghan city of Kabul," said the group's propaganda agency Amaq without providing further details.
The embassy is located in northwestern Kabul, in a neighbourhood that is home to several hotels and banks as well as large supermarkets and several police compounds.
"I heard a big blast followed by several explosions and small gunfire," said Ahmad Ali, a nearby shopkeeper.
Smoke could be seen rising from the building located in a business district of the city.
Security forces rapidly descended on the area, as the squeal of ambulance sirens rushing to the scene could also be heard.
The Iraqi embassy is located in northwestern Kabul, in a neighbourhood that is home to several hotels and banks as well as large supermarkets and several police compounds.
The attack comes a week after a car bomb attack claimed by the Taliban struck the city during morning rush hour, killing at least 35 people.
But Daesh, recently ousted from the Iraqi city of Mosul, have been expanding their footprint in eastern Afghanistan and claimed responsibility for several devastating attacks in Kabul.
A recent UN report showed that nearly 20 percent of all civilian deaths in Afghanistan in the first half of 2017 took place in Kabul.