KABUL, Afghanistan — Afghanistan’s president, Ashraf Ghani, appeared increasingly trapped in recent months in a demoralizing swim against the political tides — until President Trump, inadvertently, tossed him a lifeline.

Mr. Ghani, in pursuit of a second five-year term, had pushed for presidential elections later this month. But the only game in town that seemed to matter was the high-energy American push for a deal with the Taliban that could end the 18-year United States military presence and, ultimately, bring the Afghan sides into a power-sharing arrangement.

Not only was Mr. Ghani’s government largely left out of the initial peace efforts, a deal would almost certainly have meant the end of his time at the palace, at least for the time being, given the likelihood that a transitional government would be put in place.

Then, just when Mr. Ghani’s aides appeared to be running out of options, their bags packed for a Camp David meeting meant to complete the American agreement with the Taliban, President Trump pulled the plug on the talks. All of a sudden, the elections were back in the front seat, seemingly certain to take place on Sept. 28, with Mr. Ghani one of the favorites.