TRIPOLI, Libya — At least 40 people were killed Friday in the deadliest violence in Libya’s capital since the end of the bloody 2011 revolution that ousted Moammar Gaddafi, state media reported.

Nearly 400 others were injured, the Associated Press reported Saturday, citing Justice Minister Salah al-Marghani.

The clashes, which erupted after militias reportedly opened fire with machine guns on protesters calling for their exit from Tripoli, underscored Libya’s deteriorating security two years after Gaddafi’s death, as bands of former rebels, now solidified into well-armed militias, increasingly vie for power in this vast North African state.

The central government is weak and rife with divisions; ministers and other lawmakers have at times been linked to the militias, which control enormous stockpiles of heavy weapons and command the loyalties of thousands of young men. Efforts to absorb the militias into a national army and police force have foundered, with former rebels taking salaries from the state but rarely following its instructions.

Instead, the militias have increasingly used the threat of force to intimidate politicians, government officials and each other, even pushing through legislation — and more recently, shutting down oil infrastructure to pressure the state into meeting their demands.

More than 30 people died in the eastern city of Benghazi in June when one powerful government-sponsored militia, the Libya Shield 1, clashed with armed protesters and another state-sponsored group, the Libyan

special forces. The city was also the scene of an attack on a U.S. diplomatic mission in September 2012 that left four people dead, including U.S. Ambassador

J. Christopher Stephens, and sparked the first mass protests against the militias.

On Friday, hundreds of protesters marched on the Tripoli neighborhood of Gharghour, demanding the departure of several militias from the coastal city of Misurata that have occupied villas and streets in Gharghour since the revolution. The neighborhood once housed some of Gaddafi’s top officials, as fighting forced the regime from the capital more than two years ago.

Militias from other cities and other Tripoli neighborhoods also occupied buildings and infrastructure across the capital. Some have since disbanded or gone home, while others have forged alliances and deadly rivalries that increasingly threaten the stability of the state. Many Tripoli residents blame the former Misurata rebels based in Gharghour for setting off deadly clashes last week with a powerful Tripoli force, known as the Supreme Security Committee, inflaming tensions between the capital’s local fighters and Misurata.

Rumors circulated on Friday night that a convoy of gun trucks had left Misurata en route to Tripoli. It was unclear whether the convoy represented an escalation or a rescue force. Meanwhile, Tripoli fighters allied with the central government set up checkpoints across the capital and at key entrances to the city.

Men who participated in Friday’s protest, organized in part by the head of Tripoli’s local council, said Friday that the militias opened fire on them with heavy machine guns and rocket-

propelled grenades as the demonstrators entered Gharghour.

“They were shooting directly at us. They were shooting from the streets. They were ready for us,” said Khaled Attwibi, an information technology engineer for Libya’s elected parliament. “There were kids with us,” he added.

Libyan television footage

shot early in the confrontation showed men running, carrying bodies, as they retreated down a street, pursued by the militia fighters in trucks who directed bursts of gunfire from the antiaircraft guns mounted in their truck beds.

In the footage, some of the retreating men are carrying assault rifles.

The crisis quickly escalated. An hour after the violence erupted Friday afternoon, other gun trucks bearing the label of the Joint-Security Operations Force and other militias funded by the central government clogged the streets into Gharghour, their fighters loading their mounted machine guns in traffic and pulling on black ski masks.

The nearby Zawiya Street hospital quickly flooded with the wounded. Screams and shouts filled its narrow halls as medics crashed into one another in their haste to get stretchers to the emergency room.“Oxygen!” a group of medics shouted as they rushed a man through with blood spilling from his head.

“I was there from the beginning. We were protesting the weapons,” said a 16-year-old boy who gave his name as Haitham, as a doctor treated a shrapnel wound in his thigh. Most of the wounded were young men, but some were elderly or middle-aged. At least one of the dead was a young girl, half of her head and brain ripped away by a high-caliber bullet.

In another room, four bodies lay strewn across a blood-smeared floor and stretchers, along with crumpled hospital sheets and discarded plastic utensils.One young man appeared to be staring at the wall with lifeless brown eyes, a hand torn messily from a wrist, deep shrapnel wounds in his upper thigh.

Libyan Prime Minister Ali Zeidan called on citizens to “stay calm and avoid armed groups” to “prevent escalation” of the conflict.

“The government is working by all means to pull the fuse on this crisis,” Zeidan said at a televised news conference Friday night. He said Libya’s Interior Ministry and military police were on the scene “to protect the citizens in their protests” and to keep general security.

With dozens of militias claiming allegiance to or receiving funding from Libya’s Interior and Defense ministries, alongside the remnants of the former government’s security forces, it is often difficult to distinguish exactly which fighters are involved in Libya’s sporadic episodes of violence.

As night fell in Tripoli, the fighting appeared to intensify, with explosions and gunfire shaking the windows of nearby homes, as more militias loosely allied with the central government and angry civilians rushed to the scene with more arms.

“I think that this means that, at some level, the situation has reached a tipping point,” said Hanan Salah, a researcher for Human Rights Watch in Libya, who surveyed the devastation.

Salah and Libyan civilians worried Friday night that the fighting could spin out of control in the absence of a central government capable of holding the perpetrators of crimes accountable — or preempting revenge attacks by its well-armed citizenry.

“Unless very tough measures are taken instantaneously, there is a very high chance of this escalating further,” Salah said.“These groups have been operating with total impunity for the past two years,” she said.

The trouble is, she added, “this is not an easy task.”