LOS ANGELES — President Trump woke up here in the land of earthquakes on Wednesday morning, but he was 2,500 miles away from the tremor that was really shaking his party.

While the president hobnobbed on Tuesday night with wealthy donors in the exclusive enclave of Beverly Park, the voters in the suburbs south of Pittsburgh were in revolt, giving the Democratic candidate a narrow victory in a special election in Pennsylvania that was taking on outsize proportions.

Just as they did outside Birmingham and Montgomery, Ala., in December, and Richmond, Va., and Washington in November, energized and angry suburban voters were swamping the Trump stalwarts in the more rural parts of those regions, sending a clear message to Republicans around the country.

While Republican turnout in a district that Mr. Trump won in 2016 by 20 percentage points was healthy, Democrats showed again that they could tap unions and other traditionally friendly groups to get their voters out in droves. The N.A.A.C.P. helped win Attorney General Jeff Sessions’s former Alabama Senate seat for Doug Jones in December. Organized labor, once seen as fractured and feckless in the Trump era, gave the Democrat Conor Lamb his edge in Pennsylvania.

Rick Saccone, the Republican candidate who wrapped himself in Mr. Trump’s cloak and drew the president to his district last weekend in a bid to rescue a faltering campaign, trailed Mr. Lamb, a former Marine seeking to show his party can compete even in red territory. Mr. Lamb held an apparently insurmountable lead of 641 votes on Wednesday, with about only 500 absentee, provisional and military ballots remaining to be counted, according to county election officials.

Video

Conor Lamb Tops G.O.P. Rival in Pennsylvania

Conor Lamb, a Democrat, narrowly won a House special election in a Pennsylvania district that President Trump won by 20 percentage points in 2016.

By CHRIS CIRILLO, AINARA TIEFENTHÄLER and ROBIN LINDSAY on Publish Date March 14, 2018.
Photo by Brendan McDermid/Reuters.
Watch in Times Video »

The victory may yet be contested, but whether Mr. Lamb holds on to officially win the House seat matters less than the fact that he was so competitive in the first place. The rebuke of Mr. Trump came from a part of western Pennsylvania that overwhelmingly supported him in 2016 and that typically would not seem likely to turn to a Democrat. The district was seen as so strongly Republican that the Democrats did not even field a candidate in recent years.

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 Wednesday on Twitter or at any public appearance. But during a closed-door fund-raiser outside St. Louis late in the day for Josh Hawley, a Republican running for Senate, Mr. Trump argued that he had helped Mr. Saccone come back from a seven-point deficit to a virtual tie. Moreover, Mr. Trump asserted that Mr. Lamb’s strong showing was really a validation of the popularity of the president’s own policies.

“The young man last night that ran, he said, ‘Oh, I’m like Trump. Second Amendment, everything. I love the tax cuts, everything.’ He ran on that basis,” Mr. Trump said, according to The Atlantic, which obtained an audio recording of the event. “He ran on a campaign that said very nice things about me. I said, ‘Is he a Republican? He sounds like a Republican to me.’”

While he searched for the best possible interpretation, 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 the House this year.

Photo

Rick Saccone, the Republican congressional candidate, spoke to supporters in Elizabeth Township, Pa., on Tuesday night as results came in that showed him behind in a race that was too close to call.Credit
Jeff Swensen/Getty Images

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. Only after the election did the president pivot to argue that, in fact, Mr. Lamb was something of a quasi supporter.