State Sen. Brian E. Frosh participates in a debate at the University of Maryland's Van Munching Hall in College Park on May 19. Frosh knocked off two challengers to win Tuesday’s Democratic primary for attorney general. (Nikki Kahn/The Washington Post)

State Sen. Brian E. Frosh swept to a come-from-behind victory Tuesday after trailing badly just four months ago in the three-way contest for the Democratic nomination for Maryland attorney general.

Frosh, 67, was down double digits in the polls behind Del. Jon S. Cardin (Baltimore County), a race that tightened considerably in recent weeks and turned contentious. The Bethesda lawyer and longtime lawmaker rallied late, with weighty endorsements and a broad network of supporters working to elevate his profile statewide.

With nearly 100 percent of precincts reporting, Frosh captured about half of the votes, compared to 30 percent for Cardin and 20 percent for Del. Aisha N. Braveboy (Prince George’s). Cardin, 44, gave a short speech thanking his supporters but declined an interview.

Frosh’s victory came from commanding vote totals in the state’s larger districts in the Washington and Baltimore metropolitan areas. He was on pace to beat Cardin in his own back yard, stealing Baltimore County.

“I will fight like hell for justice,” Frosh said in a victory speech after receiving the concession call from Cardin. “I’m going to do my best to protect Marylanders and improve their lives.”

The race grew increasingly acrimonious ahead of the primary, with Frosh assailing Cardin’s legislative attendance record, judgment and qualifications. The soft-spoken senator turned up the rhetoric during debates to make sure voters didn’t confuse Cardin with his popular uncle, Ben Cardin, who represents Maryland in the U.S. Senate.

A 17-percentage point gap between Frosh and Cardin in February narrowed to just six points in June, within the seven-point margin of error, according to two Washington Post polls.

On Tuesday, Cardin won Maryland’s less-populous counties in the western and southern regions of the state, but ultimately he took only about three in 10 votes across the state. Braveboy, 39, also trailed heavily but outpaced both opponents in her home county, Prince George’s.

Frosh will now face Republican Jeffrey N. Pritzker and Libertarian Leo Wayne Dymowski in the general election, and Frosh is the clear front-runner given the large number of registered Democrats in the state.

As attorney general, Frosh said he wants to ensure that the laws he helped craft, such as firearm safety measures, are operating as designed. Making sure Marylanders are breathing clean air and drinking clean water also are top priorities.

As Maryland’s top lawyer, the attorney general will advise the next governor on legal matters and run the government agency that serves as the state’s largest law firm, handling criminal appeals and providing legal representation to state agencies.

Attorney General Douglas F. Gansler (D) left the job open after he began his run for governor.

On the campaign trail, the candidates vowed to protect citizens from threats new and old.

During his nearly 30 years in Maryland’s General Assembly, Frosh’s career touched nearly every facet of law, from consumer protection and cleaning up the Chesapeake Bay to civil rights. Frosh scored a litany of endorsements from members of Congress, unions and community organizations, touting his direct approach to problem-solving.

As chairman of the Senate Judicial Proceedings Committee, Frosh was instrumental in passing legislation on gun control, same-sex marriage and the death penalty.

Legislators lauded his work ethic and dependability on tough issues. Gov. Martin O’Malley (D) said he leaned on him to help pass a 2007 measure adopting stricter vehicle emissions standards to reduce pollution.

But Frosh had struggled to match the gravitas the Cardin name brought to the campaign. Frosh’s father also was a public servant — a longtime Montgomery judge — but few voters outside the county knew Frosh or his record at the campaign’s outset.

Frosh blanketed the state with signs, radio and television commercials, and appearances with politicians, including Rep. Elijah E. Cummings, Prince George’s County Executive Rushern L. Baker III and O’Malley.

He also took a frontal assault against Cardin, arguing that the delegate was unsuitable for the job. When news broke that Cardin had missed 75 percent of his committee votes, Frosh seized on it, declaring at every debate and forum that he would never abandon his job.

Cardin’s acceptance — and swift rejection — of a Baltimore rapper’s endorsement prompted Frosh to question his judgment.

Toward the end, Frosh turned to social media to motivate voters. Unions funded an attack ad against Cardin, digging up a 2009 episode in which he used Baltimore police resources to propose to his then-girlfriend. Cardin, at the time, repaid the city and apologized.

Cardin returned fire, calling Frosh out for rejecting legislation aimed at protecting women and children. His campaign distributed printouts of Frosh’s votes against measures including Jessica’s Law, which set mandatory minimum sentences for sex offenders.

On his YouTube page, Cardin posted a series of video chats with activists and legislators who bemoaned the power Frosh wielded in the Senate. Cardin supporters wrote editorials to state newspapers blaming the senator for obstructing good bills.

Cardin identified cybercrime as the new criminal frontier, vowing to protect children and adults from online predators, scammers and bullies. During his three terms, he sponsored bills criminalizing cyberbullying and online sexual harassment.

His message attracted interest from groups outside Maryland. Florida lobbyist Dick Batchelor, whose newly formed organization, Protect Our Future, funds candidates and research focused on Internet crime. The group paid $240,000 for pro-Cardin commercials in the Washington market during the past two weeks.

Groups and individuals near and far poured money into the race. State campaign-finance records showed that more than $2 million was raised among the three candidates.

Braveboy worked to transcend her anonymity statewide but her lag in fundraising left her in third for most of the race. Obscured by the feuding between Frosh and Cardin, she tried to set herself apart as a passionate community advocate.

The two-term delegate touted her experiences helping to save an Aberdeen family’s historic home from demolition, mentoring juveniles diverted out of the justice system, and counseling homeowners underwater on their mortgages.

At debates, Braveboy was forceful in her conviction that the state had treated historically black colleges unfairly. She focused on preserving voting rights and holding businesses accountable for unjust practices.

She debated the Ni­ger­ian ambassador at the National Press Club about the kidnapping of about 300 girls in the African state and used the event to bring attention to human trafficking in Maryland.