Russian forces spent nearly a year carving a path of devastation and death in their bid to surround the city of Bakhmut in eastern Ukraine, and by March it seemed they were close to succeeding.

“The pincers are closing,” said Yevgeny Prigozhin, the founder of the Wagner mercenary group that spearheaded Russia’s bloody drive.

He was wrong. The pincers never closed, and now Ukrainian forces have pried them farther open, taking back territory north and south of the ruined city in a few days that it took the Russians many weeks to capture.

Moscow’s troops still hold most of Bakhmut itself, Ukraine’s recent gains around the city are not large, and there is no guarantee that they will last. But for the first time in months, Ukrainian soldiers are on the offensive and the momentum in the longest and bloodiest battle of the war appears to have shifted their way — at least for now.

Continued Ukrainian advances would reverse the situation of a few months ago, putting the Russians inside Bakhmut at risk of being surrounded and trapped, and would demonstrate that the deep, fortified lines the Russians have built across Ukraine can be breached. Success around Bakhmut would also provide a major morale boost for Ukraine and a serious blow to Russia, denying it the only military achievement that for months had seemed within its grasp.

The possible reversal of fortunes comes as Ukraine is preparing to mount a broader counteroffensive, aiming for a dramatic breakthrough in a war that has settled into a grueling slugfest, with much blood spilled but little ground gained. While the dynamics around Bakhmut are somewhat specific to that battle, Ukrainian commanders say they hope to build on the lessons learned there when they try to attack in other places along the 600-mile front line.

“When you retreat it is very difficult to stop,” said Col. Andriy Biletsky, the commander of Ukraine’s 3rd Assault Brigade, whose soldiers made the first breakthrough of the Russian lines last week. “When you want to advance, it is very difficult to start.”

He cautioned that he was waiting to see if “there is a chain of five, six, seven victories” before assessing the state of the fighting, but he was hopeful.

“We can say that the phase of blind defense near Bakhmut has passed and now at least there will be movements of both sides,” he said.

“Wagner’s men have entered Bakhmut like rats into a mousetrap,” the commander of all Ukrainian ground forces, Col. Gen. Oleksandr Syrskyi, told soldiers during a visit to the front on Tuesday.

General Syrskyi and other Ukrainian commanders cautioned that the fighting was still fierce and a desperate battle was still being waged within Bakhmut, where Russian forces are trying to drive the last Ukrainian defenders from the ruins of the city. Five months after first fighting their way into the city, the Russians hold about 90 percent of it.

“The enemy is advancing somewhat in Bakhmut itself, completely destroying the city with artillery,” Hanna Mailar, a Ukrainian deputy defense minister, said Tuesday night.

Ukrainian commanders want to keep a large Russian force tied down in and around Bakhmut, preventing redeployment to other areas that could soon come under attack. They said Russia was already sending reinforcements to the Bakhmut area, including tank units and fresh fighters, to try to halt Ukrainian advances.

Still, Ukrainian commanders said on Tuesday that their soldiers were continuing to move forward.

Maj. Oleksandr Pantsyrny, the commander of 24th separate assault battalion “Aidar,” said that Ukraine has “regained initiative on the flanks, to the north and south of the city.”

Konrad Muzyka, a defense analyst for Rochan Consulting, said the recent Ukrainian gains exposed “fundamental Russian weaknesses: a lack of coordination between regular Russian formations and Wagner units, poor communication and morale.”

For months, Ukraine has insisted that before starting its counteroffensive, it needs a major influx of tanks, infantry fighting vehicles and other weapons from its allies. Mr. Muzyka said it was noteworthy that Ukrainian gains around Bakhmut were achieved “without using major Western-supplied platforms, such as Bradley I.F.V. or Leopard tanks.”

Ukraine’s troops were able to drive forward about two kilometers in some directions on Sunday and Monday, Col. Serhiy Cherevatyi, a spokesman for the Ukrainian forces fighting in the east, said in an appearance on national television.

Without going into detail, he said the forward march had been uneven, with pitched battles being fought over areas about the size of three football fields in some locations. He also warned that Russian forces were still trying to mount counterattacks in places.

While his claims could not be independently verified, Russian military bloggers also have noted Ukrainian gains around Bakhmut over the past week.

Southwest of Bakhmut, Ukrainian soldiers and commanders have reported an advance through a pocket of forests near the village of Ivanivske, and they appear to be moving in the direction of Klishchiivka, a small village that Wagner mercenaries claimed on Jan. 19 after weeks of combat.

The village sits on high ground, and whichever army controls it has a commanding position overlooking important roadways to Bakhmut.

Northwest of the city, the armies appear to be fighting over control of high ground around the Berkhiv Reservoir. Without mentioning a retreat, the Russian Ministry of Defense said over the weekend that its forces were regrouping around the reservoir to “increase the strength of the line of defense.”

Colonel Cherevatyi said there were 36 different “clashes” between the opposing armies around the city over the past two days and cautioned that it was a fluid and dynamic situation.

Ukraine’s staunch defense of Bakhmut, a small city of limited strategic value, has come at a high cost, with some of its most experienced soldiers killed in action there over the past year. But it has stopped the Russians from moving on to besiege the larger cities of Kramatorsk and Sloviansk.

Ukrainian officials say it has also played a critical role in weakening the Russian military. As long as the Russian losses were greater than Ukrainian losses, officials in Kyiv have maintained, then the fight made sense from the grim perspective of battlefield math.

Now that the Ukrainians are no longer simply absorbing blows but moving forward, Ukrainian commanders and officials hope the calculus will change again and it will be Russia that needs to decide what cost it is willing to pay to hold onto a city that is being wiped off the map day by day.

Once a city of about 70,000 people in the Donetsk region, known for its sparkling wine and salt mines, Bakhmut has become emblematic of the savagery of this war.

Ukrainian military officials emphasized that what was happening around Bakhmut is still now only a partial success.

The situation inside the city has grown so dire, Ukrainian soldiers said, that their commanders are only sending in volunteers.

“If you enter Bakhmut, you must know you might not make it out,” said one soldier. Bone-tired and bleary-eyed, he did not offer his name, as he sat under a bus stop near the battered city.

His comrade said, “It’s insane to be in Bakhmut now. The shelling never stops.”

Carlotta Gall contributed reporting from Kyiv. Nataliia Novosolova and Anastasia Kuznietsova contributed research.