CAIRO — Forces of Libya's U.N.-backed government seized control of the last remaining western stronghold of militia commander Khalifa Hifter on Friday, dealing a major setback to his ambitions to wrest control of the country.

Pro-government fighters backed by Turkey reached the center of Tarhuna, roughly 40 miles southeast of the capital, Tripoli, on Friday morning after Hifter’s forces retreated, according to military commanders and security analysts. Videos posted on social media showed pickup trucks mounted with machine guns rolling into enclaves and fighters flashing victory signs.

Mohammed Gnounou, a military spokesman for the Tripoli government, said in a statement that its fighters entered the city from four directions and gave Hifter’s forces a “lesson they will not forget.” Hifter’s military office did not respond to a request for comment. 

By the afternoon, pro-government forces were reported to be pushing eastward to other Hifter-controlled areas. 

The swift capture of Tarhuna came a day after the forces of the 76-year-old strongman retreated from their last positions in Tripoli. Hifter, based in eastern Libya, launched an offensive on the capital 15 months ago in an attempt to oust the government and install himself as Libya’s ruler.

Hifter’s defeat in western Libya leaves his future uncertain but appears unlikely to end the violent contest over lucrative oil and gas resources, territory, ideology and geographical dominance. Hifter still controls Libya’s east and south, as well as many of the oil fields.

“The fall of Tarhuna spells the end of Hifter’s offensive on Tripoli,” said Wolfram Lacher, a Libya expert at the German Institute for International and Security Affairs. “He now no longer has a realistic chance to seize power.”

“This will have major ripple effects on his alliance, which was based on the idea that he would sweep to power,” Lacher added. “Now that his forces have been routed, many in his alliance will reconsider their loyalties.”

A year ago, few observers would have predicted such a stunning military reversal.

Hifter, a dual U.S.-Libyan citizen and former CIA asset who lived for years in Northern Virginia, received heavy weaponry and other military support from the United Arab Emirates, Egypt and other regional powers in violation of a U.N. arms embargo. Russia printed billions in Libyan dinars to finance his war, pay his fighters’ salaries and bribe local tribes to support him, according to diplomats and analysts. France and other European powers elevated his stature diplomatically and politically inside and outside Libya.

Hifter’s Libyan National Army (LNA), the name he gave to his militias, swept swiftly from the east, seizing the south before besieging Tripoli in early April of last year. He vowed to overrun the capital, but his forces quickly became bogged down in a stalemate as militias loyal to the Tripoli government rose up to fight.

In September, hundreds of Kremlin-linked Russian mercenaries emerged on Tripoli’s front lines, bolstering his forces. Then the Tripoli government, known as the Government of National Accord (GNA), inked deals with Turkey giving it access to Mediterranean Sea gas fields in exchange for military aid that included drones, Syrian mercenaries and armored vehicles.

By last month, GNA-aligned militias backed by Turkish drones and defense systems, took town after town from Hifter’s forces, including capturing a strategic air base. That prompted Russia to dispatch 14 fighter jets to eastern Libya, the Pentagon said, in an apparent effort to help Hifter and send a warning to Turkey.

But then the Russian mercenaries withdrew from the front lines and headed to Hifter’s eastern strongholds, apparently in a deal with Turkey, leaving Hifter’s forces vulnerable in Tripoli. And the GNA forces took advantage.

Tarhuna had been vital to Hifter’s Tripoli offensive, a crucial base where his fighters resupplied themselves and where he had the support of powerful local tribes and a brutal militia known as the Kaniyat. On Thursday, the Kaniyat fled the city and Hifter’s other alliances also unraveled, underscoring the extent to which the commander depended on his foreign backers.

“What Hifter did in the first months of 2019 will be difficult for someone else to do again,” said Jalel Harchaoui, a Libya analyst at the Clingendael Institute in The Hague.

With his defeat, Hifter could face challenges to his authority in the south and east. The commander has already alienated some eastern tribes and politicians by a recent declaration that he was in full control of eastern Libya.

“The LNA, as a defense apparatus, is unlikely to survive in [eastern Libya] without Russia’s military and political help,” added Harchaoui. “The Persian Gulf states and Egypt aren’t quite able to stop Turkey in Libya.”

But Russia never fully supported Hifter’s all-or-nothing gamble for Tripoli, analysts said. In recent weeks, Moscow appeared to be looking for alternatives to Hifter by backing the political initiative of Aguila Saleh, a prominent lawmaker for a rival government in the east.

“For Russia, a weakened Hifter dependent on Russian military support may be an even more attractive partner than a Hifter who is close to seizing power and can choose between numerous foreign sponsors,” said Lacher.

Longtime Libya watchers sense a pivotal moment.

“Hifter’s game is over, ” tweeted Peter Millett, former British ambassador to Libya. “Next steps by the main Libyan actors will be vital. There will be many voices arguing for more fighting & rejecting reconciliation. Important to bring all Libyans together behind a comprehensive plan.”

But in the scramble to dominate Libya, that will depend on the two nations that, more than ever, control Libya’s fate.

“Turkey now has an opportunity to firmly establish its influence in western Libya,” Lacher said. “It’s not yet clear whether Turkey and Russia have agreed on where the lines of their spheres of influence should be drawn. Negotiating those might involve more conflict between their Libyan proxies.”