At least 31 people have died and dozens others have been wounded after protesters attacked the headquarters of a powerful militia alliance in Libya's eastern city of Benghazi, a hospital official has said.

Fighting erupted on Saturday after dozens of demonstrators stormed a base belonging to the Libya Shield brigade, a group of militias with roots in the rebel groups that fought in the country's 2011 civil war, They are tasked with maintaining security and are aligned with the Defence Ministry.

The protesters were demanding that militias leave their camp and submit to the full authority of Libya's security forces.

Mohamed Belied, director of Benghazi’s al-Jala hospital, said on Sunday that the deaths were caused by gunshots and explosive fragments. Hospital officials also said that protesters made up most of the casualties.

Adel Tarhuni, spokesman for the Libya Shield, said one member of the brigade had died and another seven were wounded. He said there was a peaceful demonstration in front of the brigade's headquarters, before armed men infiltrated it.

In a statement on Sunday, Prime Minister Ali Zidan described the events as “sad and painful” and urged people to be cautious and exercise self-restraint.

Zidan said the protesters demanded that a checkpoint at the entrance of the town be removed and that members of the Libya Shield leave the camp so that the police and army could take over. He said full details would be announced when investigations were completed.

Ahmed Belashahr, a local activist, said: "People protested because they believe militias go against Libya's stability, which can only be achieved through a proper army and police."

However, Tarhuni defended the legitimacy of the brigade, which he said officially came under the umbrella of the defence ministry.

'Irregular coordination'

Witnesses said some of the protesters came armed. The camp was believed to house dozens of Libya Shield militiamen, while the protesters who attacked their camp were estimated to number several hundreds.

Members of the Libya Shield abandoned the camp following the attack in a move designed to defuse tension, witnesses said.

Colonel Ali al-Shikhi, the armed forces spokesman, said that the armed forces had taken control of the situation inside the camp, including protecting heavy weapons stores.

Al-Shikhi said the events were the result of “irregular coordination” between the people and the armed men in the camp, who are considered a reserve force for the Libyan army.

The fighting was the latest episode of lawlessness to hit the North African country, which is going through a rocky transition after its bloody 2011 civil war.

Security remains elusive in the country, still abundant with weapons from the war and prone to outbreaks of violence over private and political affairs.

Last September, shortly after an attack on the US mission in Benghazi in which the American ambassador and three others were killed, the city saw a huge outpouring of public anger at the militias.