The Republican primary for a Senate seat in Pennsylvania will go to a recount, with Dr. Mehmet Oz, the celebrity physician backed by former President Donald J. Trump, clinging to a narrow advantage over David McCormick, a former hedge fund executive, in one of the nation’s most intensely watched midterm contests.
Dr. Oz was leading Mr. McCormick by 902 votes as of Wednesday, according to Leigh M. Chapman, the state’s acting secretary of the commonwealth, who said that all 67 of Pennsylvania’s counties had reported unofficial tallies to the state.
The recount could lead to a series of lawsuits and challenges in the marquee primary, one that could ultimately determine control of the closely divided Senate. That legal wrangling has already begun: On Monday, Mr. McCormick filed a lawsuit demanding that undated mail-in ballots should be counted.
A victory for Mr. McCormick — a West Point graduate and the former chief executive of Bridgewater Associates, the world’s largest hedge fund — would be a significant rebuke to Mr. Trump, who supported Dr. Oz and campaigned for him in Pennsylvania. The seat will be open after Senator Patrick J. Toomey, a Republican, steps down this year.
Pennsylvania presents perhaps the best chance for Democrats to add a seat to their fragile 50-50 control of the Senate, in which Vice President Kamala Harris holds the tiebreaking vote. The seat is the only one open in a state that President Biden won in 2020. For Republicans, holding the seat would ease their path to the majority in a year when the political winds are at their back.
At a news conference in Harrisburg, Pa., the state capital, Ms. Chapman said that the recount would be conducted transparently and would allow for observers from the campaigns.
Counties can begin the recount on Friday and must start no later than June 1, she said. They must complete the process by June 7 and report their results to the state by June 8. Pennsylvania law calls for an automatic recount if the margin in a statewide race is 0.5 percent or less of the total vote.
State elections officials acknowledged that the recount could be a slog, one that they said could cost taxpayers in Pennsylvania more than $1 million.
“I know Pennsylvanians and indeed people throughout the country have been following this race attentively and are eagerly awaiting the results,” said Ms. Chapman, who was appointed to her post last December by Gov. Tom Wolf, a Democrat.
The lawsuit from Mr. McCormick asks the Commonwealth Court of Pennsylvania to allow county election officials to accept mail-in ballots from voters who turned them in by the May 17 deadline but did not write the date on the outer return envelopes.
That step is required by a state law, which Republicans have fought to preserve.
“Because all ballots are time-stamped by the County Boards of Elections on receipt, a voter’s handwritten date is meaningless,” said Chuck Cooper, Mr. McCormick’s chief legal counsel, in a statement on Monday.
But G.O.P. leaders objected to the counting of these ballots. The Pennsylvania Republican Party and Republican National Committee were expected to intervene in the case.
Ms. Chapman, who was named as a defendant in Mr. McCormick’s lawsuit, along with dozens of county election boards, said that the office’s guidance to counties was to segregate the ballots at issue and tabulate them separately, pending the outcome of litigation.
“It’s our position that undated ballots and incorrectly, wrongly dated ballots should count,” Ms. Chapman said, calling the handwritten dates immaterial.
Jonathan M. Marks, the deputy secretary of the commonwealth who oversees elections and commissions, said at the news conference that about 10,000 mail-in, absentee and provisional ballots statewide were still being adjudicated. A breakdown of whether those ballots were cast by Republicans or Democrats was not available because they were still being tabulated by the counties.
“We are proud our campaign received nearly 418,000 votes, won 37 of 67 counties, and contributed to a historic turnout with a razor-thin difference between myself and Mehmet Oz,” Mr. McCormick said.
Dr. Oz’s campaign did not immediately respond to a request for comment on Wednesday.
“I thank everyone for their patience as we count every vote,” said Ms. Chapman, a lawyer who previously led a nonprofit that promoted mail-in voting, a flashpoint in recent elections in the state.
The Republican primary, which included five major candidates, was dominated by the nearly $40 million in television ads spent by the two front-runners and their allies, much of it on attacks bludgeoning opponents.
Both Dr. Oz and Mr. McCormick tried to transform themselves from members of the East Coast elite with middle-of-the-road politics into staunch Trump supporters.
Their bitter feud opened a lane, late in the race, for Kathy Barnette, a hard-right conservative who stood out in debates, primarily for her attacks on Dr. Oz over his late-to-the-party opposition to abortion. Ms. Barnette appeared to siphon votes away from Dr. Oz, who did not have a monopoly on Trump supporters.
Trip Gabriel and Maggie Astor contributed reporting.