Democrat Keith Ellison came out on top Tuesday in the race to be Minnesota's next attorney general following a contentious race where both candidates had to respond to questions about their past.

With about 94 percent of precincts reporting, Ellison had about 49 percent of the vote to about 45 percent for Wardlow.

Ellison will be the latest in a long line of DFL attorneys general. Wardlow would have been the first Republican to hold the constitutional office in nearly 50 years. Noah Johnson was also on the ballot as the Grassroots-Legalize Cannabis candidate, although he recently threw his support to Ellison. Johnson drew nearly 6 percent of votes on Tuesday.

Interest in the normally quiet contest began to grow in June when incumbent Attorney General Lori Swanson decided not to seek re-election and instead made an unsuccessful bid for governor.

Ellison walked away from a safe congressional seat hoping to succeed Swanson, and he prevailed in the crowded DFL primary in August.

Wardlow served one term in the Minnesota House representing Eagan. He worked as a lawyer for the Alliance Defending Freedom and was criticized during the campaign for that organization's anti-LGBT stances.

Ellison's past was also a constant issue in the campaign. Days before the primary, an ex-girlfriend, Karen Monahan, went public with allegations that Ellison mistreated her emotionally and physically during their relationship.

Keith Ellison celebrates winning the Minnesota attorney general race. Evan Frost | MPR News

Ellison repeatedly denied Monahan's allegations, and an attorney hired by the Minnesota DFL Party could not corroborate her account.

Wardlow and the national consulting firm that ran his campaign hit Ellison repeatedly on the allegations. They also raised questions about other chapters in Ellison's past, including support for notorious criminals, to paint him as unfit for the state's top law enforcement job.

Opinion polls showed many voters viewed Ellison unfavorably and few voters recognized Wardlow's name. Still, Wardlow went from 5 percentage points down in a mid-September poll to 7 points ahead in a mid-October poll. Sixteen percent were undecided.

During the final days of the campaign, Ellison remained optimistic about his chances.

"We always knew this was going to be a tough race," he said. "We never thought it was going to be a cakewalk. Of course, we didn't think it would be as tough as it has been."

Another wrinkle came late in the campaign from Ryan Durant, a high school classmate of Wardlow's. Durant went public with allegations that the Republican candidate had bullied him during high school for being gay. He also told the St. Paul Pioneer Press that Wardlow mocked him for attempting to take his own life. Wardlow denied the claims and blamed Ellison.

At one point during the campaign, Wardlow told supporters he planned a mass firing of Democrats from the attorney general's office after his election.

That comment was on Ellison's mind Tuesday night.

"In the course of this race, some have said they were gonna fire folks of this party or that party," Ellison told supporters following his victory. "My pledge to you is the only criteria for working in the Minnesota Attorney General's office is if you will serve the public office."