CAIRO — Dozens of people were killed in the Libyan capital, Tripoli, on Friday after militiamen opened fire on unarmed protesters, setting off some of the worst violence in the capital since the revolt against Col. Muammar el-Qaddafi almost three years ago.

The bloodshed at the protest on Friday afternoon quickly devolved into clashes involving armed citizens and rival militias, witnesses said. At hospitals, bodies arrived mangled by heavy-weapons fire. Air force jets flew sorties over the city, as pickup trucks carrying reinforcements of fighters raced to the clashes.

The protest that led to the violence was part of a rising tide of citizen anger against Libya’s multitude of militias, made up of thousands of men who fought Colonel Qaddafi’s forces and never laid down their arms. The militias have fed Libya’s chronic insecurity, fighting among themselves while exerting control over vital installations and even resources like oil. Fighters pledge loyalty to their commanders, tribes or towns, rather than the weak central government.

Yet the nature of the violence on Friday seemed to represent an especially ominous turn for the country, dragging it back to the winter three years ago when Colonel Qaddafi’s soldiers, roaming the capital in jeeps, gunned down protesters in the streets.

A Health Ministry official told Reuters that at least 32 people had been killed and that 391 had been injured. Libya’s prime minister, Ali Zeidan, demanded an “exit of armed groups from Tripoli,” according to Reuters. But his government seems largely powerless to make demands, a point starkly illustrated last month when Mr. Zeidan was briefly kidnapped by militiamen.

Libyans have taken it upon themselves to dislodge the militias, with protests that have provoked violent responses, mostly in the eastern city of Benghazi. Witnesses said both the size of the protest on Friday and the scale of the confrontation were rare in the capital.

The protest appeared fueled by public anger at recent infighting between a local militia and fighters from the western city of Misurata. Hundreds of people marched to an area where most of the Misurata militias are based to demand that they leave Tripoli, according to Umar Khan, a freelance journalist who joined the protest.

When they were about 300 feet away, a group of fighters, with heavy weapons mounted on trucks, fired warning shots into the air. A few minutes later, they fired into the crowd. Mr. Khan said a man next to him was shot in the chest.

Hanan Saleh, a researcher for Human Rights Watch, said she saw 25 bodies in two hospitals on Friday night and several vehicles with the insignias of different militias speeding toward the fighting.