Democrat Kyrsten Sinema has won Arizona’s Senate race, emerging victorious from a brutal, drawn-out contest to defeat Republican opponent Martha McSally.

She will be Arizona’s first female senator.

Sinema and McSally both were eager to make history. But McSally couldn’t overtake Sinema’s centrist, tightly-scripted campaign that portrayed her as a pragmatic, independent-minded lawmaker.

The last Democrat to claim victory in an Arizona Senate race was former senator Dennis DeConcini, who won a third term in 1988.

Sinema, a three-term congresswoman who represents metro Phoenix in the 9th Congressional District, has a lead of approximately 38,000 votes in the ballots counted so far. She will replace retiring Republican Senator Jeff Flake, a prominent critic of President Trump who is now an object of scorn among his base.

McSally is a two-term congresswoman and former fighter pilot from the 2nd Congressional District in Tucson.

Nearly a week after the election and shortly after the AP called the race, she conceded the race in a video message with her dog.

"I just called Kyrsten Sinema and congratulated her on becoming Arizona's first female senator after a hard-fought battle," McSally said.

With President Trump’s shadow looming over the electorate, and control of the Senate at stake, it was inevitable that the contest would descended into a series of ugly, partisan attacks. And that’s exactly what happened.

Backed up by millions of dollars in campaign cash, Sinema and McSally portrayed each other as opportunists and shape-shifters. In many ways, they were both right.

Targeting Sinema’s reputation as an independent, Sinema was attacked as a radical for her history of left-wing activism. McSally blasted her for wearing a pink tutu to an anti-war protest and for supposedly denigrating members of the armed forces serving overseas. The McSally campaign bombarded television viewers with a split-screen image of Sinema, the tutu-clad protester, next to McSally, the heroic fighter pilot.

Sinema's childhood growing up in poverty became fodder for attacks, too.

The New York Times unearthed new evidence that Sinema may not have grown up without running water or electricity, as she has claimed, while living in a former gas station in Florida. In response, the Republican National Committee mocked Sinema with a punchline from Bridesmaids and branded her a liar.

The Republicans also highlighted a 2003 exchange with Sinema on a radio show discovered by CNN. While sparring with the libertarian host, Sinema retorted that she didn’t care if Americans hypothetically joined the Taliban.

"I don't care if you want to do that, go ahead,” Sinema said on the show.

In their only debate, an indignant McSally used the comment to accuse Sinema of treason.

But the Sinema of 2003 is not the Sinema of 2018.

She broke ranks with fellow Democrats to support a controversial anti-immigrant bill, Kate's Law. Named after the shooting victim Kate Steinle, who was killed by an undocumented immigrant in 2015, the bill aimed to ratchet up penalties for people who re-enter the country, but was criticized for unfairly demonizing all immigrants. (The measure stalled in the Senate.)

Sinema acknowledged that she was open to Trump’s border wall, as long as it was a part of other border security measures. She opposes the single-payer health care plan known as Medicare for All, even as other Democrats, including presidential contenders, have endorsed the proposal. And she refrained from publicly endorsing liberal Democratic gubernatorial David Garcia, who lost to Doug Ducey.

She was among the last to rebuke Brett Kavanaugh in light of the sexual misconduct allegations against Trump's then-nominee for the Supreme Court. Last month, she said she's not proud to be a Democrat.

But for Sinema and other Democrats, similar accusations could be leveled against McSally. To them, her transformation from a Trump skeptic into an unapologetic supporter was a sign that she would say anything to get elected.

To dispatch her far-right rivals Kelli Ward and Joe Arpaio, McSally embraced Trump’s policies, especially on immigration.

In a typical line of attack, the liberal super PAC American Bridge 21st Century produced an ad that used Ward's words against McSally – “Martha McSally is even worse than Jeff Flake" – to say that other Republicans don’t trust her, and neither should Arizonans.

Democrats also highlighted McSally’s revisionist history regarding her vote last year to repeal and replace the Affordable Care Act.

McSally voted for the Republicans’ American Health Care Act, which would have rolled back protections for people with pre-existing conditions under the health care law signed by President Obama. At the very least, experts say, the Republican repeal-and-replace effort would have made insurance more expensive for those with pre-existing conditions.

The mechanics of Sinema's victory were fitting. Day by day, her margin of the counted ballots increased, grinding toward the inevitable, just like the stifling days of the summer campaign when McSally couldn't seem to rattle Sinema, despite the smears and negative ads directed at the former Green Party activist.