Court of Appeals Judge Stephen R. McCullough is seen during an interview for an appointment to the Virginia Supreme Court in the House Courts of Justice Committee at the Capitol on March 9, 2016. (Bob Brown/AP)

Republicans finally defeated Gov. Terry McAuliffe in a long, unpredictable battle over who should sit on the Supreme Court of Virginia, electing a conservative judge Thursday to a slot that the governor was poised to fill on his own.

The House and Senate elected Appeals Court Judge Stephen R. McCullough to the high court, concluding a drama that has played out since summer in brash political schemes, arcane legal arguments and startling flip-flops.

McCullough, 44, is well-known and well-regarded in conservative circles and considers the late U.S. Supreme Court Justice Antonin Scalia a role model. His name was in the mix as a potential state Supreme Court nominee for years.

The vote came one day after Republicans dropped a plan to elect former attorney general Ken Cuccinelli II (R) to the bench — an effort that started out as a tactical ploy aimed at flipping a Democrat but which unexpectedly prodded the lone GOP holdout back into the party fold.

In between, the Cuccinelli gambit nearly became the dog that caught the car: Republican legislators briefly but seriously considered the possibility of putting Cuccinelli, one of the state’s most polarizing political figures, on the high court, according to interviews with five Republican insiders familiar with the effort. They spoke on the condition of anonymity to discuss internal party strategy.

Cuccinelli considered but ultimately turned down the job, which would have precluded a potential run for governor in 2017 and forced him to stop campaigning for GOP presidential hopeful Ted Cruz.

But after he privately bowed out, Republicans let the possibility of his appointment dangle in Richmond for half a day, using it as leverage to break the impasse.

The long battle over the Supreme Court seat began in July, when McAuliffe gave the first of two temporary appointments to Jane Marum Roush, a widely respected jurist from Northern Virginia with 22 years of trial-court experience.

Republicans who control the legislature, irked that McAuliffe had not consulted them about appointing Roush and eager to assert their authority, declined to give Roush a full, 12-year appointment. They let both of her temporary stints expire and planned to elevate Appeals Court Judge Rossie D. Alston Jr. instead. Virginia is one of only two states whose legislatures are empowered to choose judges.

But that effort was thwarted in the closely divided Senate, where freshman Sen. Glen Sturtevant (R-Richmond) had refused to go along with his party’s plan to unseat Roush.

Since the General Assembly session began in January, Republicans had tried to flip a member of the Senate’s black caucus to support Alston, who is black. They touted the notion that Alston would be the third sitting black justice on the high court — a first for any state, they said.

Twice the GOP appeared close to victory. Sen. Kenneth Alexander (D-Norfolk) said he was considering Alston but hours later stuck with Roush. Then Sen. L. Louise Lucas (D-Portsmouth) said she would back Alston in a deal to fill his Appeals Court slot with Portsmouth Circuit Court Judge Kenneth Melvin, a former Democratic delegate, African American and her neighbor. But McAuliffe talked her out of it.

With the session on track to conclude by Saturday, it appeared that McAuliffe might have the chance to give Roush a third recess appointment. Republicans suggested that they might not adjourn, thus preventing such a move.

On Tuesday, a Senate panel set Richmond reeling when it announced that it would nominate Cuccinelli for the Supreme Court. Senate Republicans released a statement from Cuccinelli saying he and his wife, Teiro, were praying on it. At 1:30 a.m. Wednesday, Cuccinelli texted a Republican legislator to say he and his wife had decided against it.

But it was not until after 3 p.m. Wednesday that Republicans publicly declared that Cuccinelli was out and that they were nominating McCullough instead.

In the intervening hours, Republicans tried to use the possibility of a Justice Cuccinelli to convince one or more Senate Democrats to break with their party to support Alston. It had the desired effect on Lucas, who, like many liberals, considers tea party hero Cuccinelli hostile to abortion, gay rights and climate science.

“I heard the name, I flew back over to [Sen. Ryan T. McDougle (R-Hanover)], and I said, ‘Are you all serious about Cuccinelli? This has got to be a joke.’ He said, ‘Well, we can’t get any movement on Alston and Melvin unless we get your vote.’ I told him I’d rather go with Alston and Melvin than Cuccinelli.”

Lucas said she called Melvin, who said he was interested in the job but urged Lucas to get other black Democrats to stand with her. Melvin did not respond to a request for comment.

Lucas set out to round up votes in the black caucus Wednesday morning, not knowing that Cuccinelli had bowed out overnight. When McAuliffe heard about her lobbying, he summoned Lucas and other black senators individually to his office to urge them to hold firm until the end of the session, Lucas said. McAuliffe spokesman Brian Coy declined to comment.

Lucas was undeterred, but it soon became clear that the GOP did not need her after all. Much to the surprise of fellow Republicans, Sturtevant said he was willing to vote for Cuccinelli.

Sturtevant had stood firmly against unseating Roush all session. But some Republicans said he had been quietly looking for a way out without violating his vow not to vote for Alston, whom he had called a “political pawn” in the Roush drama. A new candidate provided just that opportunity. And Cuccinelli had supported Sturtevant in his race last fall.

When Republican leaders made it clear that Cuccinelli had bowed out, Sturtevant threw his support to McCullough, who had at one time worked for Cuccinelli in the state attorney general’s office and earned Sturtevant’s admiration. Republicans moved ahead with McCullough’s nomination Wednesday and took the final vote Thursday.

“Everything happened so fast,” Lucas said. “Before we could do anything, it all just blew up.”