Provo Mayor John Curtis declared victory in the Republican primary Tuesday night against his two staunchly conservative rivals in the special election to replace retired Rep. Jason Chaffetz.

The Associated Press called the race before 10 p.m. The National Republican Congressional Committee rushed to congratulate Curtis. And Utah Gov. Gary Herbert stood by the mayor’s side as he announced his triumph.

”Let me just tell you that I wrote two speeches for tonight,” Curtis said at his Provo campaign party, ripping up a paper that held his would-be concession.

Still, with potentially thousands of votes outstanding, his closest competitor refused to concede.

“I think there’s a little too much optimism on the other side,” said former state Rep. Chris Herrod. He later added that he does “not want to be the sore loser in this. We’re just trying to figure out the data.”

Curtis held a roughly 12 percentage point lead over Herrod, according to unofficial results, collecting nearly 43 percent of the vote. In Utah County, which includes about 60 percent of the registered Republicans in the 3rd Congressional District, at least 25,000 ballots returned Monday and Tuesday had not yet been counted, according to the clerk.

Businessman Tanner Ainge, who was in third at 27 percent, didn’t wait for a second batch of votes to be tallied before saying he’d take his “skill set back to the private sector.” He added that he’s proud of the strides he made in his first political campaign, noting it was ”an uphill climb” to challenge two seasoned Republicans.

“I’m happy for [Curtis]. I think he’ll be a good representative of this district,” Ainge said at his Orem gathering.

At the Curtis party, it was all grins and cheers beginning with the earliest results of the evening at 8 p.m. that put the mayor ahead and tracked with his leads in every public opinion poll prior to the primary. He had faced a tense run-up to the GOP race, growing grittier in the past few days with insults and name-calling and big money flying in from out-of-state super PACs.

Most of the now $880,000 spent by the outside committees — with a large chunk from the conservative DC-based organization Club for Growth — funded negative advertisements pitted against Curtis.

“They said you could not survive almost $1 million of attack ads aimed at your character. I’ve got a message to those PACs in Washington, DC,” he said in his victory speech. “This is my town. This is my district. Go home, you’ve wasted your money.”

The mayor suffered weeks of hounding in the race over his party loyalty — which his competitors called into question and outside groups echoed, comparing Curtis to Hillary Clinton and labeling him a “flaming liberal” (which the mayor said once in jest). Herbert condemned the negative campaigning.

Curtis was, for a short time, registered as a Democrat when he ran and subsequently lost a bid against state GOP Sen. Curt Bramble in 2000. He wanted to bring the typically Republican stances of anti-abortion and pro-gun rights to the left and jokes now that it was “a fling on the dark side.” He later returned to the right in 2006.

His position as mayor is nonpartisan, though he’s called himself “the most conservative” that the state’s ever seen.

In his congressional bid, which he launched by gathering signatures, Curtis has criticized Republicans for not being ready with a plan to replace Obamacare “when the spotlight came on us.” And though he wrote in a “good friend’s name” instead of voting for now-President Donald Trump, the mayor wants the president to be successful and will work with the White House agenda when he agrees with it.

Kaylen Nelson, a Republican who lives in Utah County, voted for Curtis because she felt Herrod “was too conservative.”

“I didn’t want another Mike Lee,” she said, referring to the Utah senator who rode into office on a tea-party wave. “I just think he’s too polar[izing].”

Nelson believes Curtis’ previous stint as a Democrat is “interesting’’ and might help him work across the aisle better in Washington.

Andre Jones, a student at Utah Valley University in Orem, also voted for Curtis. “I like that he’s more moderate.”

The congressional primary, unlike most races in the past year, has not been defined by the candidates’ alignment with the president. Instead, the GOP runoff has been a race to the right, an attempt to establish the most conservative credentials in a state where voters were largely unsettled by Trump’s campaign.

Herrod had been the ultra-conservative — with corresponding endorsements from right-wing Republican Sens. Ted Cruz and Rand Paul. During his term in the Utah House, from 2007 to 2012, Herrod made his mark as one of the most outspoken critics of illegal immigration — and agrees with Trump on building a wall between the United States and Mexico. He won the party delegates’ nomination in June after speaking about his experience in Russia and Ukraine.

He now works as a real-estate developer and loan officer and served as the Utah director for Cruz’s campaign. In 2012, he launched — and lost — a bid to unseat U.S. Sen. Orrin Hatch and, later in 2016, state Sen. Curt Bramble, R-Provo.

Ainge’s political experience is limited to a one-year stint volunteering in the campaign finance arm of Mitt Romney’s 2008 presidential bid. He gets more of a boost from his famous father, Danny Ainge, Boston Celtics general manager and a Utah celebrity for his game-winning shot that sent BYU to the Elite Eight in the NCAA basketball tournament. And his mom, Michelle Toolson Ainge, donated $250,000 to the super PAC Conservative Utah buying ads on her son’s behalf.

The newcomer, who qualified for the ballot by collecting signatures, has worked as an attorney for most of his career and runs his own consulting firm, Ainge Advisory.

Neither Herrod nor Ainge supported Trump in the primary, but both voted for him in the general election.

In the reliably red district, where registered Republican voters outnumber Democrats nearly six-to-one, Curtis will be well-positioned to win the Nov. 7 general election. He faces Democrat Kathie Allen, who’s amassed a surprising haul of nearly $700,000, and the United Utah Party’s Jim Bennett, son of the late three-term Sen. Bob Bennett, as well as a handful of independent and third-party candidates.

The winner will serve the final year of Chaffetz’s term after the congressman surprisingly stepped down on June 30 and has since joined Fox News as a contributor. His early departure turned what would have been a municipal primary only into a harried and complicated congressional special election — the first in Utah in 90 years.

The 3rd District stretches from central Salt Lake County to the southernmost border of San Juan County. Just two of the seven counties it encompasses, Emery and Carbon, have opted for traditional polling instead of mail-in ballots.

Curtis ran a shooting range business in Provo before serving two terms as mayor of the state’s third largest city — which gave him an obvious and recognized boost in name recognition and polling. “This is one of the best run cities in the United States,” he acknowledged, before suggesting that Washington should be run the same way.

“I’m so proud that we did it the right way,” he added. “And the reason is this had to be a different campaign because I want to be a different congressman.”

— Reporters Taylor W. Anderson, Jennifer Dobner and Taylor Stevens contributed to this report.