Isamic State 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 US troops.
A top Afghan political leader, Abdullah Abdullah, was present along with other key political figures and escaped unharmed.
Some 81 people were wounded, a government spokesman said, adding that the death toll could rise.
Islamic State claimed responsibility for the attack, the group's Amaq news agency reported on its telegram channel.
The Taliban, who were ousted from power by US-led troops in 2001, denied involvement almost immediately.
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 Islamic State had also said was carried out by its militants.
"The attack started with a boom, apparently a rocket landed in the area, Abdullah and some other politicians ... escaped the attack unhurt," Abdullah's spokesman, Fraidoon Kwazoon, who was also present, told Reuters by telephone.
Broadcaster Tolo News showed live footage of people running for cover as gunfire was heard.
Afghan defence 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.
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.
Ambulances and stretchers bustled back and forth at the hospital to deliver the wounded for treatment.
"I was at the ceremony when gunshots started. I rushed toward the door to get out of the area but suddenly my foot was hit by a bullet," Mukhtar Jan told Reuters from a stretcher at the hospital.
Ali Attayee, at the hospital to support his wounded brother, said: "Those who committed this crime want to destroy our people. ... We're sorry for those committing such crimes."
Representatives of the United States, European Union and NATO condemned the attack.
"Attacking the innocent and defenceless at a memorial event is a sign of weakness, not a show of strength. ... The Afghan people deserve a future free from terror," US Secretary of State Mike Pompeo said in a statement.
The attack was one of the largest on civilians in Afghanistan in a year.
"Horrific attack in Kabul today ... heartbreaking and unacceptable. We are tired of war and violence," said Shahrzad Akbar, head of the Afghanistan Independent Human Rights Commission.
Hazaras are mostly Shi'ite Muslims. Minority Shi'ites have been repeatedly attacked by Sunni militants in Afghanistan.
The United States has sought to spearhead efforts towards a lasting peace arrangement. Violence decreased during a seven-day hold-down accord with the Taliban before last Saturday's deal, though the Taliban have since resumed attacks on Afghan forces.