Democratic incumbent Jon Tester was elected to a third-term Wednesday, defeating Republican challenger Matt Rosendale in a brutal campaign that included four visits to Montana by President Donald J. Trump and other top guns from the GOP.

The Associated Press called the race, which at $67 million as the most expensive in the state's history, Wednesday morning for Tester.

"Look, we did it," Tester said to a roomful of supporters at a Holiday Inn in Great Falls.

"This was an interesting race," he said, mentioning complaints from the campaign trail about the endless parade of political ads that flooded TV screens and radio broadcasts for the past several months.

"I would tell you our country is on the cusp of coming together but it isn't," he said, adding it may become even more divided.

He congratulated Rosendale on a hard-fought campaign. Rosendale spokesman Shane Scanlon told the AP the Republican state auditor spoke with Tester to congratulate him late Wednesday morning.

Tester noted the president's four visits to the state to campaign for Republicans as well as several trips by the vice president, the president's son and several other high-ranking Republicans.

"You beat 'em all!" somebody from the crowd shouted.

The election seemed personal as the president was angry at Tester for derailing his nomination of Dr. Ronny Jackson to head Veterans Affairs. Tester had gone public with behavior issues regarding Jackson, prompting Jackson to withdraw his nominating and sparking the president to call Tester’s resignation.

One political observer said there were a lot of forces at work.

“Well, the Tester brand was able to defeat the Trump brand -- narrowly,” said Jeremy Johnson, an associate political science professor at Carroll College.

He said Trump visiting the state four times did help Rosendale, but had its limits.

Johnson said those visits actually energized Democratic voters.

“Tester did well with independent voters and that vote was enough to win re-election,” he said.

The biggest surprise came in the form of voter turnout, Johnson said. He had not seen the final numbers but said they reflected a presidential year rather than a midterm.

Tester said Wednesday that Montana is a special state in which people do not vote for political parties but for people," Tester said.

Tester called for more bipartisanship in Congress and for lawmakers to put aside "political pettiness."

"The American people are demanding politicians work together," he said.

Tester held the lead through most of Tuesday night. Rosendale then took the lead early Wednesday and remained there until unofficial results were posted on the secretary of state's website about 9:30 a.m.

David Parker, associate professor of political science at Montana State University, tweeted late Tuesday that the Senate race numbers "are tightening because all the heavily Republican, rural counties are reporting. We still have most of the big urban areas where Democrats do well not reporting yet."

Results posted about 9:30 a.m. showed Tester with 49 percent of the vote to Rosendale's 48 percent, or a difference of 1,022 votes. That's of 73 percent of the precincts reporting.

Tester told supporters late Tuesday that the results would not be known until Wednesday.

He told people to rest up now "because we have a lot of work to do."

Rosendale now serves as state auditor and had portrayed himself as a candidate who would support Trump and the president’s policies in Washington, D.C. He also claimed Tester was out of touch with Montana values.

Tester touted himself as an effective lawmaker who despite being a Democrat was able to get 20 bills signed by Trump, many of them affecting veterans.

Recent polls labeled it too close to call.

He paused with emotion at Wednesday's rally when he mentioned his work for veterans.

Rosendale released a statement Wednesday congratulating Tester and talking about his campaign experience.

"While we suffered a setback, our movement and our cause will continue to move forward. I will never stop working to make our country and state better," he said.

He thanked his wife, Jean and three sons.

“My work for the people of Montana is not done and I will continue to serve and always put the people of Montana first,” Rosendale said.

Tester, 62, a Big Sandy farmer, recently told a rally this was “the most important election of my lifetime.”

He has never won an election with a majority and has been aided by Libertarian candidates who siphon off votes from Republicans.

His campaign had portrayed Rosendale as an outsider, pounding he was an East Coast developer who had moved to Montana from Maryland.

Rosendale, 58, who served in the state Legislature prior to being elected auditor in 2016, owns a ranch near Glendive and noted Trump won the state by 20 points in the 2016 presidential election and that Tester’s votes did not reflect its residents' wishes.

The president played a big role in this election, coming to Montana four times to support Rosendale and GOP Rep. Greg Gianforte. Those stops included Billings, Bozeman, Great Falls and Missoula.

Not only has Trump visited the state, but Vice President Mile Pence has come as well as has Donald Trump Jr., former Fox News personality Kimberly Guilfoyle and Sens. Rand Paul of Kentucky and Sen. Lindsey Graham of South Carolina.

Actor Jeff Bridges has stumped for Tester and the Band Pearl Jam had a concert supporting him in August. One of the band members, Jeff Ament, is also from Tester's home town of Big Sandy.

Tester said Wednesday he hoped Trump would return to Montana, not for political rallies but to see the challenges facing the state.

He also thanked his volunteers for their help.

"I thank each and everyone of you you should be sleeping," Tester said.

In the Senate, Tester sits on committees that include Appropriations, Veterans Affairs, Indian Affairs, Commerce, Science and Transportation and Banking, Housing and Urban Affairs.

A map posted on the Montana secretary of state's website based on preliminary results noted Tester had won eight counties Tuesday, six counties were undecided and the rest of Montana's 56 counties had voted for Rosendale.

Tester has never won a senate seat with a clear majority, pulling in 49.2 percent in 2006 to win over GOP incumbent Conrad Burns, who got 48.3 percent of the vote. In that 2006 race, Libertarian Stan Jones got 10,377 or 2.5 percent of the vote.

In 2012, Tester, seeking his second term, was in a race that some declared also a “tossup seat,” and won with 48.6 percent of the vote over GOP challenger Denny Rehberg, who was serving in the U.S. House. Libertarian Dan Cox took 6.5 percent of the vote.

Overall, Tester won 16 counties, which were the more populous counties in the state. Among them were Cascade, Lewis and Clark and Chouteau counties. Four years later Trump won those very same counties.

Despite Tester's victory Wednesday, Republicans remained in control of the Senate. Democrats, however, became the majority party in the House.

Reach Phil Drake at 406-422-0772 or pdrake@greatfallstribune.com

Who won?

For election results go to: https://sosmt.gov/elections/results/ or https://www.greatfallstribune.com