ISIL gunmen opened fire at a ceremony in Kabul on Friday, killing at least 32 people in the first major attack in the city since the United States reached an agreement with the Afghan Taliban on a phased withdrawal of U.S. troops.
Some 81 people were wounded, a government spokesman said, adding that the death toll could rise.
ISIL claimed responsibility for the attack, the group's Amaq news agency reported on its telegram channel.
The Taliban, who recently expressed willingness to deescalate the Afghan civil conflict, denied involvement almost immediately.
A top Afghan political leader, Abdullah Abdullah, was present along with other key political figures and escaped unharmed.
Afghan defense forces continued to fight gunmen throughout the day, finally securing the area by killing about three gunmen in the late afternoon, according to ministry of interior spokesman Nasrat Rahimi.
The gathering marked the 25th anniversary of the death of Abdul Ali Mazari, an ethnic Hazara leader who was killed in 1995 after being taken prisoner by the Taliban.
Several people were killed in a similar attack on the same commemoration last year, which ISIL had also said was carried out by its militants.
President Ashraf Ghani tweeted that the attack was "a crime against humanity and against the national unity of Afghanistan."
Abdullah was runner-up in the last three Afghan presidential elections, each of which he disputed. He has served as chief executive of a coalition government since 2014 and is also a former foreign minister.
Ghani said he had telephoned Abdullah, his longtime political rival. Abdullah is contesting an Electoral Commission announcement last month declaring Ghani the winner of September's presidential election.
Dozens of relatives gathered at the morgue of a hospital not far from the blast, with many breaking down in tears as they waited to identify their loved ones.