At least three soldiers were killed as special forces clashed with armed activists in the eastern port city of Benghazi early Saturday, the army reported.
The interim leader of Libya's army warned of the danger of a "bloodbath" as the unrest in the city flared up again a week after clashes there killed more than 30 people.
At around 4:00 am (local time) heavy gunfire and explosions could be heard near the headquarters of the special forces, not far from the city centre, an AFP journalist and witnesses reported.
The Facebook page of the special forces said its members were exchanging light arms and rocket fire with an "outlaw" group and reported that two of its fighters had been wounded.
Then a little later it posted photos of three of its members and announced that they had been shot dead as they "were defending the legitimacy of the state with courage and honour".
The clashes came just hours after dozens of protesters forced a brigade of former rebel fighters from their base in Benghazi on Friday evening.
The protesters had burned two vehicles belonging to the First Infantry Brigade, before moving on the barracks, said a Libyan army officer.
The brigade was forced to quit its headquarters as the protesters took it over.
A witness on site said the protesters, some of whom were armed, had fired in the air and launched a rocket-propelled grenade at an external wall of the barracks, though nobody was hurt.
The brigade was set up by former rebel fighters who helped overthrow the regime of Muammar Gaddafi in 2011. It says it takes its orders from the defence ministry.
Following that first attack, Salem al-Konidi, the interim head of the army said on the Al-Aseema television channel late Friday: "If the special forces are attacked, there will be a bloodbath...
"There could be a catastrophe in Benghazi."
He said he did not know who had launched the attack or what their motive was, but appealed to the city's elders and dignitaries to act to try to head off the unrest.
Konidi is the interim replacement for General Yusef al-Mangoush, who resigned last Sunday, a day after fighting in Bengazhi killed 31 people and left dozens more wounded.
He had come under mounting criticism for having failed to form a national army in the face of resistance from militias unwilling to surrender their independence.
In last week's violence, armed demonstrators opposed to the city's militias attacked the "Shield of Libya" brigade at its barracks.
The Shield of Libya is mostly made up of rebels who battled dictator Kadhafi in 2011, and it is formally under defence ministry control.