WASHINGTON — Republican congressional leaders perched on the couches in the Oval Office froze in mid-smile on Wednesday afternoon when they realized President Trump was bypassing them to cut a short-term spending and debt ceiling deal with Democrats, and not them.

Speaker Paul D. Ryan and Senator Mitch McConnell, the majority leader, had expected the rare bipartisan, bicameral meeting to be little more than a photo-op in which the broad outlines of a deal were discussed, aides said.

Instead, Mr. Trump — who has often outsourced the details of negotiations to subordinates like Vice President Mike Pence — interrupted Treasury Secretary Steven Mnuchin as he argued for an 18-month debt ceiling increase to stabilize financial markets. When other leaders offered yearlong or half-year extensions, the president waved them off, according to the accounts of staff members briefed on the meeting.

The three-month deal Mr. Trump eventually embraced is a significant tactical change for a president thus far anxious to preserve, not expand, his political appeal. The plan was pitched by two Democratic leaders he has relentlessly demonized and marginalized: Senator Chuck Schumer of New York and Representative Nancy Pelosi of California.

The deal will give Democrats increased leverage in coming negotiations, several senior members of Mr. Trump’s team lamented after the meeting.

In his nearly eight months as president, Mr. Trump has tacked almost exclusively to the right to solidify his conservative populist base, earning him the limited, but granite, support of 30 to 40 percent of the public. In the past 36 hours, he seems to have abruptly abandoned that strategy, driven by the immediate need to pass spending bills after Hurricane Harvey — and chastened by the bipartisan backlash against his decision on Tuesday to phase out a program that allowed unauthorized immigrants brought to the United States as minors to remain here.

“If we can get something to happen, we are going to sign it, and we’re going to make a lot of happy people,” Mr. Trump told reporters en route to Bismarck, N.D.

The Oval Office meeting produced a moment that legislators and staff members described as instantly iconic, and not in a good way.

Mr. Trump often invites his daughter Ivanka Trump into meetings to signal their conclusion — or to keep his interlocutors off balance. When Ms. Trump entered the office toward the end of the discussion on Wednesday, ostensibly to discuss tax reform, Republicans in the room reacted with astonishment and annoyance.

Mr. McConnell, who is barely on speaking terms with the president, quietly seethed, according to two people familiar with the situation. He breezed past other lawmakers and staff members in purse-lipped silence when he returned to the Capitol.

“The president agreed with Senator Schumer and Congresswoman Pelosi,” said Mr. McConnell, who tried to project a sense of bipartisan unity despite the jarring shift.

Mr. Ryan coolly told colleagues that he had come to expect such surprises. But he complained that Mr. Trump had failed to fully consider the implications of his actions.

A chastened Mr. Mnuchin left the room, in what one witness described as a state of shell shock. Later, he told reporters he could not have been “happier” with the meeting.

The president’s fiscally conservative budget director, Mick Mulvaney, told Republican lawmakers later that he would do his best to cope with the new reality that his boss was dealing with the opposition.

Kevin Cramer, North Dakota’s at-large Republican congressman, told reporters traveling with Mr. Trump to Bismarck that he was gobsmacked by the Schumer-Pelosi deal.

“I gasped when I heard it,” he said. “In fact, I sought clarification when the president told us before the flight.”

Republicans have banked on at least one comforting constant during Mr. Trump’s tenure: However unpredictable, disengaged, backbiting or belligerent he has been to them, he has been unwilling to ditch them for the Democrats.

Until Wednesday.

Late Tuesday night, he vowed to “revisit” his decision to scrap the popular Obama-era Deferred Action for Childhood Arrivals program if Congress does not come up with a permanent fix within six months, confounding immigration hard-liners who had been celebrating victory hours before.

Earlier in the day, Mr. Trump had scrapped his predecessor’s program sparing younger illegal immigrants from deportation on the grounds that a president does not have the power to take such action by himself. He then put the onus on Congress by giving it a six-month deadline to “fix” the program before it would expire.

Then, barely eight hours after his decision was announced, the president went on Twitter with a message that completely undercut both positions in just under 140 characters. “Congress now has 6 months to legalize DACA (something the Obama Administration was unable to do),” he wrote, using the initials for the program. “If they can’t, I will revisit this issue!”

White House officials offered no immediate explanation about what Mr. Trump intended by the message or how he would revisit the issue in six months. Asked during an appearance on Wednesday morning with congressional leaders if he had any second thoughts on his decision, Mr. Trump said, “No second thoughts.”

But administration officials said the move greatly undercut the position of another key cabinet official, Attorney General Jeff Sessions, a longtime proponent of tougher enforcement of immigration laws, who announced the move.

Mr. Trump watched the cable news coverage of the DACA decision on Tuesday in frustration at the widespread condemnation of the policy, even among Republicans. He told aides that most of the feedback he had received was positive.

But by Wednesday, Mr. Trump seemed to grasp just how perilous his political situation had become. That was why he pushed for a quick resolution to the spending and debt ceiling negotiations, saying he wanted to return focus to Hurricane Harvey aid.

But the president also relished the opportunity to confound critics who accused him of abandoning campaign promises to govern as a bipartisan, deal-cutting businessman.

Mr. Trump has enjoyed surprisingly good relationships with Ms. Pelosi and Mr. Schumer over the years. “Nancy — you’re the best,” the president scrawled with a black Sharpie over a news account of her swearing-in as the first female speaker in 2007, which he had framed and sent to her.

Mr. Trump, a native of Queens, has known Mr. Schumer, who was raised in Brooklyn, for years, and both men view themselves as outer-borough scrappers. For all of their public displays of mutual disdain, the men have been communicating through their staffs for months, and seem to enjoy each other’s company.

They enjoyed a quick joke and a backslap at the start of Wednesday’s meeting, captured by news photographers shooting through an Oval Office window. By Wednesday night, it was making the rounds on social media and the conservative news media as proof that the president had surrendered to the enemy.