Jihadist group Ansar al-Sharia attacked Libyan special forces Monday in the eastern city of Benghazi, sparking a battle in which at least eight people were killed and dozens wounded, officials said.
The bloodshed came a day after US Secretary of State John Kerry vowed to work with the international community to try to restore order in Libya which has been hit by growing unrest.
Military governor Colonel Abdullah al-Saidi declared a "state of alert" and ordered all soldiers to report for duty, in what was the first such confrontation between the army and the heavily armed jihadist group.
The government warned that the army was a "red line" not to be crossed and called in a statement for calm, urging Benghazi residents to cooperate with loyalist forces.
Nine people were killed and 49 wounded, it said, although a health ministry official later gave AFP a casualty toll of eight dead and 51 wounded.
Medics at Benghazi's Al-Jala hospital have said at least five soldiers were among the dead.
There was no immediate casualty toll for the jihadists, whose were being treated in a clinic run by Ansar al-Islam.
A spokesman for the special forces told AFP the fighting erupted early Monday after a patrol was attacked near Ansar al-Sharia's headquarters.
"The army retaliated, sparking clashes with all types of weapons," said the spokesman, Colonel Milud al-Zwei, adding that fighting lasted for several hours and spread to other districts of Benghazi.
Explosions and gunfire were heard in several neighbourhoods, an AFP journalist said.
Witnesses said gunmen had set up checkpoints on roads leading into Benghazi to prevent reinforcements reaching the Islamists and that angry residents torched a jihadist office.
Calm returned gradually in the afternoon, with some shops reopening and motorists and pedestrians back on the streets, an AFP journalist said.
Tribal and civil leaders were trying to mediate a solution between the warring sides to avoid further bloodshed.
Sharia Islamic law
Signup to our newsletter and follow us on Facebook and Twitter!
Ansar Al-Sharia emerged after the 2011 fall of Moamer Kadhafi's regime, with its military wing composed of former rebels.
Blamed for the murders of judges and security personnel in Benghazi, it is also suspected of responsibility for a September 2012 attack in which the US ambassador and three other Americans were killed.
It denies any involvement.
The group, which demands implementation of sharia Islamic laws as the sole source of legislation, controls areas of Benghazi as well as Sirte and Derna, also in eastern Libya.
Libyan and foreign analysts say jihadists are responsible for much of the violence in the east, but that the government has been loath to take on the heavily armed groups for fear of reprisals.
Ansar al-Sharia has itself said it does not recognise state institutions or its security forces, even as the government struggles to integrate former rebels into a regular army and police.
In a statement earlier this month, the group insisted security in Libya was "dependent on the application" of Islamic law, and branded security services "taghuts", evil forces at the service of tyranny.
The Benghazi violence comes as the authorities take steps to evacuate militias from Tripoli, on the back of popular discontent in the capital against armed groups.
On November 15, 46 people were killed and more than 500 wounded in Tripoli after militiamen opened fire on peaceful demonstrators calling for them to leave the city.
In similar protests in September last year, Benghazi residents managed to dislodge Ansar al-Sharia from its headquarters, only for the jihadists to return a few weeks later.
On Sunday, Kerry held talks in London with Libyan Prime Minister Ali Zeidan and British Foreign Minister William Hague to discuss the growing unrest.
Kerry later said the talks focused on how Washington and London can "help Libya to achieve the stability that it needs".
Ex-rebels who helped topple and kill Kadhafi in 2011 have banded into militias and carved their own fiefdoms, each with its own ideology and regional allegiance.