WASHINGTON — President Trump struck a deal with Democratic congressional leaders on Wednesday to increase the debt limit and finance the government until mid-December, blindsiding his own Republican allies as he reached across the aisle to resolve a major dispute for the first time since taking office.

The agreement would avert a fiscal showdown later this month without the bloody, partisan battle that many had anticipated by combining a debt ceiling increase and stopgap spending measure with relief aid to Texas and other areas devastated by Hurricane Harvey. But without addressing the fundamental underlying issues, it set up the prospect for an even bigger clash at the end of the year.

In embracing the three-month deal, Mr. Trump accepted a Democratic proposal that had been rejected earlier in the day by Speaker Paul D. Ryan of Wisconsin. Mr. Trump’s snap decision at a White House meeting caught Republican leaders off guard and reflected friction between the president and his party. After weeks of criticizing Republican leaders for failing to pass legislation, Mr. Trump signaled that he was willing to cross party lines to score some much-desired legislative victories.

The deal to keep the government open and paying its debts until Dec. 15 represented an extraordinary public turn for the president, who has for much of his term set himself up on the right flank of the Republican Party. But it remained unclear whether Mr. Trump’s collaboration with Democrats foreshadowed a more sustained shift in strategy by a president who has presented himself as a master dealmaker or amounted to just a one-time instinctual reaction of a mercurial leader momentarily eager to poke his estranged allies.