The jihadist Islamic State group conceded losing control of the city of Derna in eastern Libya and promised to "avenge" its fighters in a video posted on Twitter.
The 10-minute video posted late on Saturday by the "media office of the province of Barqa" in Libya confirmed the loss of Derna after clashes.
The coastal city near the border with Egypt has often been described as the bastion of IS supporters in the north African country since its fighters first entered Derna in November 2014.
It has seen fierce fighting in recent weeks between the jihadist group and members of the Mujahedeen Council of Derna.
Its gunmen ousted IS from many areas of the city controlled for more than a year by armed groups including Ansar al-Sharia, which is close to al-Qaeda.
In the video recording, a man in military uniform promises to avenge the IS members who died in the fighting.
Senior middle east analyst Michael Nayebi-Oskoui at US-based global intelligence firm Stratfor said the loss of Derna does not greatly change IS's effectiveness in Libya.
"IS being kicked out of Derna does not dramatically alter the capabilities or effectiveness of the IS in Libya, as the group has faced and will continue to face stiff opposition from local competitors," he said.
"IS remains constrained in its ability to retaliate for any potential loss in Derna."
"That said, attacks (including suicide attacks) against security checkpoints in and around Sirte, Benghazi and Tripoli are all possible," Nayebi-Oskoui added.
After the fall of dictator Muammar al-Gaddafi's regime in 2011, Libya now has two parliaments and two governments vying for power, one based in Tripoli and one in Tobruk in the east.
Only the latter is recognised by the international community.
IS, which already controls large swathes of territory in Iraq and Syria, has exploited the chaos in Libya, notably taking control of the city of Sirte, just 450 kilometres (280 miles) east of Tripoli, in June.