KABUL, Afghanistan — Two gunmen opened fire on a crowded event in Kabul attended by the opposition leader Abdullah Abdullah on Friday, and officials said at least 32 civilians were killed and dozens wounded. The attackers struck Afghanistan’s capital less than a week after the United States and the Taliban signed an agreement aimed at ending the 18-year-old war.

Mr. Abdullah escaped injury in the attack, his aides confirmed. But it took Afghan security forces five hours to hunt down the assailants, who had used a nearby high-rise to fire into the crowd, before the government declared the attack over.

The country’s health ministry said at least 58 people were wounded. Dawod Danish, head of the main hospital nearby, said 26 bodies and 20 wounded people had been brought to his facility alone.

The attack came at a particularly delicate time in Afghanistan. Political tensions have been at high pitch in recent weeks, with Mr. Abdullah challenging the results of an election that declared the incumbent president, Ashraf Ghani, the winner of another five-year term in office. Mr. Abdullah also declared victory, and had threatened to hold a rival inauguration at the same time as Mr. Ghani’s if it went ahead as scheduled.