CAIRO — Libyan fighters aligned with the United Nations-backed unity government on Thursday battled their way toward the center of Surt, the Islamic State’s coastal stronghold, in a rapid offensive that has confounded expectations and upended Western strategic calculations.

Only a few months ago, American generals were touting a plan to dislodge the Islamic State from Surt with a campaign of airstrikes. Yet the three-week-old Libyan ground offensive, led by a brigade from the nearby city of Misurata, has gradually closed in from the west and south, capturing an airfield, military bases and a traffic junction where the Islamic State has hanged dozens of people.

The swiftness and extent of these successes and the seeming inability of the Islamic State to hold on to territory has surprised observers inside and outside Libya.

Clashes continued on Thursday as the Islamic State, whose force in the city is estimated to be in the low thousands, fought back with suicide bombings and sniper fire. Mohamed al-Ghassri, a spokesman for the Misuratan force, said 110 fighters from his side had been killed and more than 500 wounded since the operation started.

Western officials and analysts cautioned that the Islamic State could yet mount counterattacks or guerrilla strikes. But many were impressed with the momentum of the assault, which comes as the Islamic State, also known as ISIS or ISIL, faces escalating pressure in Iraq and Syria. The assault also dealt a heavy blow to the Islamic State’s ambitions for a permanent base on the southern shores of the Mediterranean.

“The question now is whether ISIS will bunker down in the city center, or try to find a way out,” said Mattia Toaldo, a Libya analyst at the European Council on Foreign Relations.

Videos circulating online showed jubilant Libyan fighters tearing down a metal frame, called the “stage of horror,” that the Islamic State used to hang at least 49 people. Fighters flashed victory signs as they sped around the city’s outskirts, even as fighting continued.

In Washington, American officials voiced guarded optimism.

“We welcome the advancement of G.N.A. forces against ISIL in Surt, and the coordination of the groups combating ISIL under the G.N.A.’s authority,” said Lt. Col. Michelle Baldanza, a Pentagon spokeswoman, referring to the Government of National Accord, or unity government. “We are not going to predict timelines on Libyan military operations.”

Islamic State fighters began filtering into Surt in 2014, taking advantage of the factional chaos in Libya in the years after the downfall of Col. Muammar el-Qaddafi. That vacuum eventually allowed the extremist group to control 150 miles of coastline and establish pockets of territory in a number of other cities. But now it controls no more than 40 miles of coast, and has been diminished elsewhere.

A small contingent of American and British Special Operations forces are playing a background role in the assault on Surt. Mr. Ghassri, the spokesman for the Misuratan force, said that about 25 American and British soldiers were “gathering intelligence and providing logistical information” for the operation.

The clashes on Thursday were concentrated around a conference hall known as the Ougadougou Center. The Libyans’ modest fleet of warplanes bombed the hall, where fighting continued on Thursday night. Yet the main part of Surt — a knot of tightly spaced streets where the former dictator, Colonel Qaddafi, was raised and, in 2011, killed — remains in the hands of the Islamic State.

In Misurata, 110 miles to the west, hospitals said they had been overwhelmed by wounded soldiers, some of whom were flown to Italy and Turkey for treatment, Reuters reported. Suicide bombings had inflicted the heaviest casualties, in one instance killing 32 people, Mr. Ghassri said.

Earlier this year, Pentagon officials estimated the Islamic State’s strength in Libya at up to 6,500 fighters, although other Western estimates run as low as 3,000. In a report on human rights violations in Surt published last month, Human Rights Watch cited a Libyan military intelligence official who estimated 1,800 fighters inside the city, 70 percent of whom were foreigners.

Tunisians account for the majority of foreign fighters in Surt. Other recruits come from across North and West Africa, Syria and Afghanistan. The Human Rights Watch report detailed horrific scenes from everyday life, including public beheadings and floggings, that were said to have undermined support for the Islamic State among local residents.

The military assault is good news for the unity government and the United Nations, which helped establish it. But in Libya, even good news can carry potential political risks. The unity government has failed to establish its authority since its arrival in the capital, Tripoli, in March. It has relied heavily on support from the United States and its European allies, which have imposed sanctions on opposing politicians, in an effort to bolster its standing.

But some of the militias now fighting together have previously fought one another, raising fears about what would happen if they managed to capture Surt. “The key thing is what happens the minute they put their flag on ground,” said Mr. Toaldo, the analyst. “We’re talking about two forces that were fighting each other in the same area only one year ago.”

A version of this article appears in print on , Section A, Page 7 of the New York edition with the headline: Libyan Fighters Backed by U.N. Press Toward the ISIS Stronghold of Surt. Order Reprints | Today’s Paper | Subscribe