CAIRO — Libyan militias backed by American airstrikes said they have cleared the stronghold of the Islamic State in Libya, a defeat that would set back the group’s ambitions in North Africa. The country, however, remains very unstable amid battles between rival militias, and the remaining militants could still undermine a fragile U.S.-backed unity government, analysts say.

Libyan fighters erupted in celebration in the coastal city of Sirte on Tuesday after a nearly seven-month struggle to oust the Islamic State, as the mostly pro-government forces were searching for any remaining militants.

The Islamic State’s hopes of extending its self-proclaimed “caliphate” beyond Syria and Iraq into Libya have been dashed, at least for now. But while their propaganda war and recruiting efforts have also been weakened, analysts said, the group remains active in other parts of the country.

Libya faces the specter of clandestine cells staging terrorist attacks, much like they’ve done recently in Iraq, Syria and Afghanistan after battlefield reverses in those countries.

“The retaking of Sirte is certainly a negative blow to Islamic State affiliates in Libya because they will no longer have a territorial stronghold in the country,” said Claudia Gazzini, senior Libya analyst for the International Crisis Group. “This is significant because it no longer gives them the possibility to operate in the open or to recruit and levy taxes directly.”

Libyan forces backed by U.S. airstrikes finish clearing the last Islamic State holdout in Sirte after a six-month battle for the militant group’s former North African stronghold. (Reuters)

Yet despite its demise, Gazzini said, “we cannot rule out” that the group will carry out attacks in other parts of the country.

Libya descended into turmoil following the killing of dictator Moammar Gaddafi in 2011 in the aftermath of the country’s Arab Spring uprising. The oil-producing nation fractured into fiefdoms controlled by competing groups of former rebels. Rival governments were established in the capital, Tripoli, and in the east, with each claiming authority.

In the vacuum, the Islamic State and other extremist groups emerged in Libya. In early 2015, in alliance with other radical factions, the Islamic State’s affiliate seized parts of Sirte, situated in the heart of Libya’s oil crescent. Within months, the militants had tightened their grip on the city and sought to create their own government.

The militants viewed Sirte as a possible fallback capital if their headquarters in the Syrian city of Raqqa fell. Recruits from other parts of Africa and the Middle East — as well as within Libya — streamed to Sirte, which was where Gaddafi was born.

“Operationally, it was an important base and logistics hub for other ISIS fronts in Libya,” said Frederic Wehrey, a Middle East analyst with the Carnegie Endowment for International Peace.

The fall of Sirte arrives at a tense moment for Libya. Rival militias have been battling for territory and economic gain in recent days in the worst eruption of fighting in the capital in more than a year. Clashes are also underway in Benghazi, Libya’s second-largest city, pitting Islamists against forces loyal to Gen. Khalifa Hifter, a powerful commander who recently seized control of key oil terminals, while his rivals were focused on besieging Sirte.

The violence has further challenged the authority of the unity government, which has struggled to exert control since it arrived in Tripoli in March. Those challenges are poised to become even more complicated as the Islamic State plots its next moves — and a possible revival.

“For it to find a place to govern and rule, it would need to find a locale suffering from marginalization and some sort of tribal or social constituency that would welcome it — as well as a preexisting jihadist infrastructure,” Wehrey said.

Today, some Islamic State militants are fighting in Benghazi. Analysts say that others who fled Sirte may have relocated to southern Libya, including the city of Sebha. The south offers a haven to regroup, but its remoteness in the Libyan Desert makes it more difficult for it to attract recruits and stage attacks.

There have also been reports of Islamic State activity in western Libya, including in the town of Bani Walid, where sympathetic tribes have provided the militants with safe houses, and in the coastal town of Sabratha, where the Islamic State has strong links to smuggling and other criminal networks.

“It’s hard to predict whether they still have a leadership capable of thinking up next steps,” said Mattia Toaldo, a Libya analyst and senior policy fellow at the European Council on Foreign Relations. “The real question is whether the single jihadists will find a new ‘home’ in other jihadist groups.”

Tripoli could become the militants’ next target. Violence in the capital would further undermine the government’s authority and dissuade foreigners from returning. Tensions are also growing between Islamist militias in the city, and any eruption of fighting could aid Islamic State cells.

“Any outright conflict between these groups would certainly throw in disarray what little security there is in Tripoli, and this could open space for radical groups like IS,” said Gazzini, referring to the Islamic State.

Even as the unity government seeks to capitalize on the liberation of Sirte and bolster its international legitimacy, many of its supporters are increasingly dissatisfied.

The militias that ousted the Islamic State are mostly from the city of Misurata. Many Misuratans say the recent clashes in Tripoli are an attempt by their rivals to claim the capital.

“The Misuratans are not happy with this and might try to mobilize forces to Tripoli in the upcoming weeks,” Gazzini said.

Or they could end up fighting Hifter’s forces, Toaldo said.

The more the instability, the greater the possibility that the Islamic State will regroup and that other extremist groups will form in Libya.

“The important thing to underscore is that jihadism and the radical threat in Libya is still very much present, especially given the institutional breakdown of the state, the alienation of vast swaths of youths, marginalized towns and tribes,” Wehrey said.