At least 27 people have been killed in an attack, claimed by the ISIL (ISIS) group, at a ceremony in the Afghan capital where a top Afghan political leader, Abdullah Abdullah, was present but escaped unharmed.

While a health ministry spokesman confirmed the death of 27 people, a NATO source told Reuters news agency that the death toll was slightly higher. More than 30 people were killed and 42 wounded, 20 of whom were in a serious condition, the agency reported.

More:

The ISIL claimed it killed and injured 150 people, according to the group's Amaq website, without providing evidence.

It is the deadliest attack since a peace deal was signed last week between the United States and the Taliban that aims for the complete withdrawal of US and NATO troops within 14 months. The war in Afghanistan is the longest one in US history, which continues for more than 18 years.

It was also one of the biggest attacks on civilians in Afghanistan in a year.

"The attack started with a boom, apparently a rocket landed in the area, Abdullah and some other politicians ... escaped the attack unhurt," Fraidoon Kwazoon, Abdullah's spokesman, was quoted as saying by Reuters.

President Ashraf Ghani condemned the attack and called it "a crime against humanity". He also said he had telephoned Abdullah, his longtime political rival who is contesting last month's Electoral Commission announcement that declared Ghani the winner of September's presidential election.

The video of attack when Mohammad Karim Khalili, was giving a speech. pic.twitter.com/EZglGlGiZg — Tamana Ashna (@tamanaashna) March 6, 2020

Broadcaster Tolo News showed live footage of people running for cover as gunfire was heard.

Al Jazeera's Hoda Abdel-Hamid, reporting from Kabul, said a standoff between Afghan security forces and the attackers lasted for nearly six hours.

The difficulty in quelling the attack "really underlines that the Afghan security forces will be left in a fragile situation" after the foreign troop withdrawal, Abdel-Hamid said.

Meanwhile, the Taliban denied a role in the attack on the gathering marking the anniversary of the death of Abdul Ali Mazari, an ethnic Hazara leader who was killed in 1995 after being taken prisoner by Taliban fighters.

Despite the deal between the Taliban and the US, fighting has continued to rage across the country, casting a pall over hopes that the agreement would lead to a reduction in violence.

Several people were killed in a similar attack on the same commemoration last year. The ISIL had claimed responsibility for that attack also.

'We're sorry for those committing such crimes'

Dozens of relatives gathered at the morgue of a hospital not far from the blast site, with many breaking down in tears as they waited to identify their loved ones.

Ambulances and stretchers bustled between the scene of the attack and the hospital to deliver the wounded for treatment.

"I was at the ceremony when gunshots started. I rushed towards 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, told the news agency: "Those who committed this crime want to destroy our people at this juncture in society, we're sorry for those committing such crimes."

Representatives of the US, the European Union and the NATO condemned the attack.

"We strongly condemn today's vicious attack...We stand with Afghanistan for peace," the US charge d'affaires in Kabul, Ross Wilson, wrote on Twitter.

Horrific attack in Kabul today that led 2 tens of civilian casualties. Heartbreaking & unacceptable. We r tired of war & violence. @AfghanistanIHRC findings indicate more than 10,000 civilian casualties in 2019. First tangible step towards peace 4 Afgs would be ending d violence. — Shaharzad Akbar (@ShaharzadAkbar) March 6, 2020

Shaharzad Akbar, head of the Afghanistan Independent Human Rights Commission, said: "Horrific attack in Kabul today ... heartbreaking and unacceptable. We are tired of war and violence."

Hazaras are mostly Shia Muslims. Minority Shias have been repeatedly attacked by Sunni fighters in Afghanistan.