And while Mr. Saccone carried the most Trump-supporting counties along the West Virginia border, Mr. Lamb made just enough inroads in those rural areas to give the voters in suburban Allegheny County the chance to deliver the Democrat the slimmest of leads.
Rarely shy about weighing in on other news of the day, Mr. Trump made no mention of the race on Twitter on Wednesday morning. Instead, he left it to an aide to find the silver lining. Raj Shah, a White House spokesman, told reporters on Air Force One en route to St. Louis that Mr. Trump actually helped the Republican candidate and asserted that the Democrat’s showing was really a validation of the popularity of the president’s policies.
“The president’s engagement in the race turned what was a deficit for the Republican candidate to what is essentially a tie,” Mr. Shah said. “Also the Democrat in the race really embraced the president’s policies and his vision whereas he didn’t really embrace Nancy Pelosi, the Democratic leader.”
The stinging message could hardly have been more pointed for a Republican president mired in low approval ratings, burdened by investigations and facing the growing likelihood that Democrats may seize power in Congress later this year.
Mr. Lamb, 33, defied political geography and appeared on the verge of capturing the state’s 18th District despite a torrent of Republican money and Mr. Trump’s personal intervention. At a rally Saturday, Mr. Trump mocked Mr. Lamb as “Lamb the Sham,” promised that Mr. Saccone would “vote for us all the time,” and rambled about his own achievements as he sought to transfer his own political success to the Republican candidate.
In the end, none of it seemed to be enough. Democratic enthusiasm appeared to overwhelm a part of the state that has long been a Republican stronghold. For the president, the vote is an ominous echo of Democratic victories in Virginia and Alabama, where his political efforts were shrugged off or counterproductive.
The tally was also a blunt rejection of the president’s political calculation that tax cuts and steel tariffs would persuade voters in a region once dominated by the steel industry to embrace the Trump agenda on behalf of Mr. Saccone. “Steel is back,” he repeatedly said at the rally, apparently to little effect.
A Republican victory in Pennsylvania might have helped deflect attention from the continuing collapse of the president’s inner circle, which Tuesday included Mr. Trump’s abrupt firing of Secretary of State Rex W. Tillerson and the forced resignation of John McEntee, one of Mr. Trump’s closest personal aides, who is under investigation for financial crimes and was marched out of the White House.
Instead, Mr. Saccone’s lackluster performance was a grim bookend for a day in which the president’s trip to the Mexico-California border to view wall prototypes was completely overshadowed by the churning turnovers in his national security team.
Mr. Trump and Republican Party leaders had desperately sought to head off an outcome that was once thought of as politically impossible. Conservative groups spent more than $10 million in the hopes of defeating Mr. Lamb, who received similar help from Democratic politicians like former Vice President Joseph R. Biden Jr.
A barrage of Republican advertisements condemned Mr. Lamb as a “Rubber Stamp for Nancy Pelosi,” the Democratic leader in the House. One flier sent to voters showed Mr. Lamb firing an assault weapon, an attempt to weaken the Democrat’s support among liberal voters. A deceptive video purported to show Mr. Lamb in a fight with labor unions.
But in recent weeks, polls in Pennsylvania consistently showed Mr. Saccone’s popularity slipping, and Mr. Lamb gaining traction. Visits by Vice President Mike Pence and other White House officials did little to buck up the party’s candidate.
By the time Mr. Trump arrived in Moon Township, Pa., for the rally, the race had tightened significantly, and many White House and Republican Party officials were already worried that he was lending his support to a lost cause.
As it turned out, they may have been right. During the rally, Mr. Trump called Mr. Saccone “an extraordinary person” and dismissed Mr. Lamb as someone who should not be trusted by voters in western Pennsylvania.
“The people of Pittsburgh cannot be conned by this guy Lamb, because he’s not going to vote for us,” Mr. Trump said. He also added a half-dozen presidential tweets over the last week.
On Tuesday night, as he huddled in the home of Edward Glazer, an owner of the Tampa Bay Buccaneers, and his wife, Shari, he had nothing to say on the matter.