DOYLESTOWN, Pa. — Pennsylvania voters, who went for President Trump two years ago, swerved in the other direction Tuesday, handing Democrats key victories and swelling the party’s share of the state’s House delegation.

Democratic women crashed the all-male congressional delegation, winning four House seats in the Philadelphia suburbs, according to The Associated Press.

Three of the Democratic candidates — Mary Gay Scanlon, a lawyer; Chrissy Houlahan, an Air Force veteran; and Susan Wild, a former solicitor in Allentown, Pa. — flipped seats previously held by Republicans, an achievement aided by a new map of House districts drawn this year to eliminate a Republican gerrymander. Additionally, Madeleine Dean, a state representative, won a seat vacated by a Democratic man.

In western Pennsylvania, a fifth Democrat, Representative Conor Lamb, who faced an incumbent Republican representative in a new district, also won his race. Democrats netted three flipped seats, because Guy Reschenthaler, a Republican running in Mr. Lamb’s vacated district, also won his race.

Democrats were all but preordained to net the two suburban Republican seats won by Ms. Scanlon and Ms. Houlahan because of the redistricting. Ms. Wild’s victory in the Seventh District, which includes the Lehigh Valley, was a bit of icing on the cake, as the state contributed to Democrats’ new House majority.

Still, the party went into Election Day hoping for an even broader advance, a gain of up to six seats, riding the wave of anger at Mr. Trump among the diverse, college-educated voters of the Philadelphia suburbs — especially women.

Republican incumbents held off strong challenges in three close races from Erie County in the northwest to Bucks County in the southeast. Representatives Mike Kelly, Scott Perry and Brian Fitzpatrick were all returned to the House. Mr. Fitzpatrick won a second term in his Bucks County-centered seat, the lone Republican win in the Philadelphia region.

“I lost some really good friends tonight,” Mr. Fitzpatrick, one of a vanishing breed of Republican moderates, told supporters, referring to the party’s loss of the House. “We had a rough night tonight nationally.”

As widely expected, Democrats’ top two elected officials in the state, Gov. Tom Wolf and Senator Bob Casey Jr., cruised to re-election.

Republican strategists had openly worried that their statewide candidates’ failure to appeal to moderates — Representative Lou Barletta, running for Senate, and Scott Wagner, the nominee for governor — would be a hurdle for many Republican voters and hurt the party’s down-ballot candidates.

Mr. Wagner, a wealthy businessman and state senator from York County, was a divisive figure who modeled himself after Mr. Trump. At one point he posted a Facebook video threatening to stomp into Mr. Wolf’s face with golf spikes.

Mr. Wolf, a liberal, has raised state spending on education and expanded Medicaid benefits under the Affordable Care Act, though he lost a battle with Republican majorities in the General Assembly to impose a severance tax on natural gas.

Mr. Casey’s opponent, Mr. Barletta, built his career on opposition to illegal immigration. But throughout his campaign he struggled to benefit from the issue in a state Mr. Trump won in 2016, even as the president’s final election message focused on an “invasion” by amigrant caravan, rhetoric designed to turn out the Republican base.

Image

Campaign signs for Senator Bob Casey and Gov. Tom Wolf, both Democrats, in Collegeville, Pa., last week. Both won re-election on Tuesday.CreditErin Schaff for The New York Times

In purple Pennsylvania, those voters are more or less balanced out by suburban Republicans, who find the president’s harsh nativism repugnant. Here as elsewhere, Republican suburban women were more inclined to vote their values than their stock portfolios.

Mr. Trump, who spent the final weeks campaigning nonstop in red states where Democratic senators were vulnerable, including multiple visits to Florida, Indiana and Missouri, last came to Pennsylvania on Oct. 10 to stump for Mr. Barletta in Erie.

The House race considered the closest in the state was Mr. Fitpatrick’s, as he fought to portray himself as independent of the national party. He won endorsements from organized labor and gun control groups and was the beneficiary of around $7 million in outside spending by national Republican committees.

His Democratic opponent, Scott Wallace, a liberal philanthropist, told supporters the midterms were about “that crazy guy down there” in the White House.

At a high school in Warrington in Bucks County, it was clear that the backing of the liberal groups had boosted Mr. Fitzpatrick. Lindsay Edling, a 22-year-old waitress voting for the first time, said she generally prefers Democrats, but voted for Mr. Fitzpatrick because he had the support of two gun control groups. “Just all the shootings going on, it’s really been concerning,” she said.

And Susan Novick, 52, a special-education teacher, said she had voted for Mr. Fitzpatrick because his name was on the list given by the Pennsylvania State Education Association.

But it was also clear that Mr. Trump’s aggressively divisive campaign motivated some Republican and independent voters to cast a ballot for Mr. Wallace to send a message. “Don’t even ask me about Trump,” sneered Dorothy Brodsky, 73, a Republican who voted for Mr. Wallace. “I have 18 grandchildren. I’m very concerned about their future.”

Barbara Krumins, who runs a construction business with her husband and is registered as an independent, said the congressional race was “100 percent” a referendum on the president.

“Just everything about him has been total anxiety and anguish,” she said. “Everything that comes out of his mouth is just crap. It’s just insane. It’s not the views I was bought up on.” She voted for Mr. Wallace.

The biggest factor in congressional elections in the state, unquestionably, was redistricting. In January, the Pennsylvania Supreme Court struck down the map of Republican-drawn districts used in the previous three House elections.

The court, with a majority of Democratic justices, ruled the map had “clearly, plainly and palpably” violated the state’s Constitution.

The new court-approved map was more faithful to the nearly equal balance of voters between the parties. Registered Democrats outnumber Republicans five to four in Pennsylvania, but in the last three congressional elections under the old maps, Republicans won 13 of 18 House districts.

After Tuesday, the delegation is split 9 to 9, reflective of Pennsylvania’s continuing role as a battleground state going into the 2020 presidential election.

A version of this article appears in print on , on Page A22 of the New York edition with the headline: Pennsylvania Democrats Pick Up House Victories. Order Reprints | Today’s Paper | Subscribe