A homicide bomber struck Thursday in Pakistan on the holiest festival for Shiite Muslims, triggering a riot that left a provincial town in flames and at least 27 people dead and more than 50 wounded.

After the bombing, which appeared to be a sectarian attack, security forces battled enraged worshippers who torched shops and cars and took up positions on hills overlooking Hangu, where the sound of gunfire echoed through the smoky streets.

In neighboring Afghanistan, hundreds of Shiites and Sunnis clashed in the western city of Herat, hurling grenades and burning mosques. At least five people were killed and 51 wounded.

The Shiites were marking Ashoura, when they pound their chests and flail their backs with chains and blades to mourn the 7th-century death of Imam Hussain, a grandson of the Prophet Muhammad. Hussain's death fueled a rivalry between Shiites and Sunnis over who should succeed the prophet.

In Iraq, tens of thousands of Iraqis marched and beat themselves in blood-soaked processions through the holy city of Karbala, but no holiday-related violence was reported amid tight security to prevent Sunni Arabs from attacking the event, as they have the previous two years, killing more than 230 people.

Sectarian attacks have also often marred the annual rite in Pakistan, but rarely in Afghanistan. Two years ago, a suicide attack on a Shiite procession by Sunni militants in the southwestern Pakistani city of Quetta killed 44 people.

Pakistan declared a curfew and deployed its army to restore order in Hangu, a market town of about 200,000 people.

District police chief Ayub Khan said 23 people died in the bombing and riots that followed. Three other three men and a woman died in a separate shooting on a minibus on the outskirts of town, according to a commander of the paramilitary Frontier Constabulary, Aziz ur-Rahman. Security officials said Shiites protesting the bombing had blocked a road and fired on the vehicle.

Akram Durrani, the top elected official in the province, said a preliminary investigation showed the attack in Hangu was a suicide bombing, but he gave no further details.

Witnesses said a procession of about 300 people in black mourning clothing had come out of the Imam Barga Quami, a Shiite mosque, at about 9:45 a.m. and were passing the town bazaar when the explosion went off.

"The procession started and we were beating our chests. All of a sudden there was an explosion in the procession," said Asar Hussain, 45, who suffered shrapnel wounds to his torso, head and legs. He believed it was a suicide attack but did not see the bomber.

Vegetable vendor Mohammed Jamil said panic followed.

"Some people rushed to the injured and dead bodies, others went to houses and took out weapons and knives and kerosene oil and started setting fire to shops, destroying everything," he said.

An Associated Press reporter who evaded police roadblocks to enter Hangu saw most of the bazaar destroyed, some shops still in flames and thick smoke drifting through the streets. Soldiers patrolled the streets, some in armored personnel carriers.

There was an occasional sound of gunfire, and sometimes the boom of heavier weapons, as troops fired at protesters who took up positions on hilltops surrounding the town.

"Curfew has been imposed. People should stay in their houses and not come out," a police officer announced through a loudspeaker mounted on a van in Hangu.

Helicopters landed at a police training college in the town and took 27 of the more than 50 wounded away for treatment at an army base.

In Afghanistan, where like Pakistan, about 80 percent of the population are Sunnis and most of the rest Shiites, sectarian violence swept through Herat.

The fighting followed three days of rioting in Afghanistan over cartoons of the Prophet Muhammad published in Europe. Those riots left 11 dead.

Thursday's violence in Afghanistan began after 300 Sunnis threw stones at a Shiite mosque, said Ismatullah Mohammed, a senior police officer. Shiites retaliated by attacking a camp for displaced people. Violence spread, with both sides throwing grenades, burning about a dozen cars and two mosques, he said.

Mohammed said Islamic extremists were suspected of inciting the violence, and security forces fanned out across Herat, blocking roads into the city amid reports hundreds of young men were coming from outlying areas to join the fighting.

Dr Barakatullah Mohammedi at Herat Hospital said at least five people were killed and 51 wounded.