LYMAN, Ukraine — As dusk gathered on Sunday, Elena Kharkovska stood in the courtyard of her apartment block, contemplating what she had just learned: Without ever moving, she had supposedly lived in Russia for one day.

President Vladimir V. Putin decreed on Friday that four regions of Ukraine — including the province of Donetsk, which includes Ms. Kharkovska’s hometown, Lyman — had been annexed into Russia.

But before the news could reach her, Ukrainian soldiers were in control of the city again, as Russian forces retreated.

Without electricity, radios or the internet, residents of the city of Lyman said, they were unaware of the grandiose ceremony Mr. Putin held at the Kremlin on Friday to celebrate an annexation that the world largely condemned as a sham.

“I didn’t hear anything about it,” Ms. Kharkovska said as she watched a kettle of buckwheat simmer on a campfire. The town has been without cooking gas for months.

“I’m in shock,” she said, laughing. “Nobody told us anything” on Friday about how her town had supposedly been grafted onto Russia, or on Saturday, when Ukrainian troops took it back.

“It’s funny to me because it recalls a saying, ‘Without me, they married me,’” she said.

The dizzying speed with which Russia claimed to have owned and then lost Lyman encapsulates the energy of Ukraine’s monthlong blitz to recapture territory from Moscow. The liberation of Lyman, a strategic rail hub, is not just an important military victory, but also a potent symbol of Ukraine’s plucky ability to publicly humiliate a larger and ostensibly more powerful foe.

Ukraine has only a brief window to expand its counteroffensive before soldiers mobilized in a call-up Mr. Putin ordered last month will reinforce Russian positions in Ukraine. Then, once again, Ukrainian cities in the areas that Moscow now considers sovereign Russian territory could revert to Russian control.

President Volodymyr Zelensky of Ukraine and Western leaders have called Russia’s annexation claims meaningless and illegal. The territory, they say, is and will remain Ukrainian. The Ukrainian Army’s swift capture of Lyman in the hours after Mr. Putin’s speech highlighted the Russian leader’s precarious hold on the so-called annexed territories. In a hasty withdrawal, the Russians left behind official documents, military vehicles and the bodies of their soldiers.

The retreat highlights the weakness of the Russian Army, Ukrainian and Western military analysts say, but it also portends an elevated risk of escalation. A humiliated Russia may respond by deploying thousands of newly recruited soldiers or employing its nuclear arsenal, the world’s largest.

The Ukrainian Army, outgunned and outnumbered by the Russians, captured Lyman through a strategy of attacking nearby roads and villages, and never engaging in a head-on fight for the city, said Serhiy Hrabsky, a former colonel in the army.

Fighting in the pine forests and on both sides of the Oskil River, the Ukrainian forces moved from village to village, systematically shutting off every Russian route for resupply or retreat, he said. For the Russians, a journey through the pine forest became a lethal gamble.

“The key success of the Lyman operation is that we left behind any idea of head-on assault,” Colonel Hrabsky explained. It was only a matter of time, he added, before Russian soldiers in Lyman ran out of ammunition and fuel.

When I arrived in the city on Sunday, the dense forest surrounding the town bore the signs of a ferocious weekslong artillery battle. Severed branches were scattered across the road. Whole villages along the route were in ruins.

“Look at the destroyed houses,” said Roman Plakhaniv, a lieutenant in the Kramatorsk District’s police force, who arrived on Sunday to patrol the city. “This was a nice, normal town. People from another country came and destroyed it.”

In the city, fall leaves blew about the streets. Stray dogs loped between the houses. Bread trucks remained parked in the lot of demolished bakery, waiting morning distributions that would never come.

The police said about 5,000 of the city’s prewar population of 22,000 remained.

On Sunday, Ukrainian soldiers and police patrolled the streets, searching for Russian stragglers. Occasional explosions could be heard as demining teams detonated land mines. Otherwise, the city seemed firmly under Ukrainian control.

In front of the mayor’s office lay a heap of Russian propaganda posters, apparently freshly torn down and partly burned in a fire. Decorated in the white, blue and red of the Russian tricolor, they were soggy from the rain. One explained the significance of Russian state symbols: the flag and the national anthem.

Inside City Hall were notices explaining how to apply for building permits under the occupation authority and phone numbers to call to apply for a Russian pension, suggesting plans by Russian bureaucrats to put down roots.

Copies of a newspaper called Donetsk Republic were scattered about the floor. An edition published on Sept. 15 had an article headlined “Defense of the Republic and Borders of Russia,” apparently intended to tamp down worry as Ukraine’s counteroffensive gained ground.

The article explained that “the president of Russia, Vladimir V. Putin, announced that in the course of the special operation, Russia is not losing military strength and will defend its sovereignty.”

The detritus of the Russian retreat, and Ukraine’s yellow and blue flags flying overhead, exposed the falsity of the paper’s claims, as well as the lies of the president it was quoting.

In addition to clearing land mines, initial efforts at restoring control would focus on investigating war crimes and fixing gas and electrical utilities, Ukrainian officials said.

At one point, on the road into town, I passed what appeared to be the remains of a Ukrainian attack on Russian soldiers trying to flee the city in a civilian van. The van’s doors were open, and sleeping bags, pads, military coats, rations, shoes and other supplies had spilled onto the road. Nearby, on the side of the road, were anti-tank mines and the bodies of half a dozen Russian soldiers.

A Ukrainian demining team checked to see if they had been rigged with explosives: The Ukrainians attached ropes to the bodies to tug and jostle them from a distance, lest they explode.

Asked how the Russians had died, one soldier shrugged and said, “They came to a foreign land.”

Maria Varenikova contributed reporting from Kramatorsk, Ukraine.