Former Maryland lieutenant governor Anthony G. Brown beat former two-term state’s attorney Glenn F. Ivey in Maryland's 4th District Democratic primary on April 26. (WUSA9)

Former Maryland lieutenant governor Anthony G. Brown, fresh off a stunning loss in the most recent gubernatorial race, won the Democratic nomination for Maryland’s 4th Congressional District on Tuesday.

Brown beat popular former two-term state’s attorney Glenn F. Ivey. Del. Joseline Peña-Melnyk (Prince George’s) was a distant third.

After his defeat by now-Gov. Larry Hogan (R) in 2014, Brown’s return to political life was fraught with doubts that he could muster the will, money and momentum to win the nomination against a field of formidable competitors.

“This campaign has always been about giving back to the community where my wife and I raise our family, and where I have dedicated my life to public service,” Brown said in a statement. “I also want to acknowledge and thank my opponents in this race for their hard-fought campaigns and for focusing on the real issues at hand here in the 4th Congressional District.”

In the Republican primary, longtime Prince George’s County activist George E. McDermott beat a crowded field and will face off against Brown in November. The district’s voter registration is overwhelmingly Democratic — by about 4 to 1.

April 26 Maryland primary results

It has been a secure seat for Democrats since the district was redrawn in 1991 into a majority-minority voting district. U.S. Rep. Donna F. Edwards has represented it since 2008.

The district includes large swaths of Prince George’s and a part of Anne Arundel County. Brown won both counties Tuesday.

For Brown, a 54-year-old retired soldier, an attorney and former legislator, this was a campaign of redemption and reclamation, he told supporters. Although his opponents raised more money, Brown used a last-minute personal investment and called on volunteers to help cover the polls, buy media ads and deliver a message to voters that his pedigree, experience and personality were suited for Congress.

The race to succeed Edwards, who lost her bid Tuesday for the Democratic Senate nomination, quickly focused on three major candidates in the Democratic primary. Brown, Ivey and Peña-Melnyk raised the most money, received the most endorsements and garnered the most attention.

Ivey was the first to declare his candidacy shortly after Edwards jumped into the race for U.S. Senate to replace retiring Sen. Barbara A. Mikulski (D-Md.). And he led the field early in fundraising and got key endorsements, including that of County Executive Rushern L. Baker III (D).

He was the first candidate to raise $1 million dollars, to run advertisements on radio and on local and cable TV, and to develop a policy platform that emphasized building and preserving the legacy of President Obama, who is hugely popular in Prince George’s.

With his wife, former state Del. Jolene Ivey, and his six children helping, Ivey used his record as a two-term prosecutor and as a staffer on Capitol Hill to appeal to voters in the Democrat-rich communities.

Ivey’s warm, folksy manner and Southern upbringing charmed older reliable voters in the district’s south and central regions as he went from neighborhood to neighborhood with his vow to take on the Republican majority in Congress. His campaign focused on reminding voters of his record of fighting domestic violence, setting up mentoring programs for youths and prosecuting police misconduct when, he said, few others would.

Brown targeted the same communities as Ivey. He served eight years, beginning in 1999, as state delegate from District 25, a cluster of predominantly African American inner-Beltway neighborhoods and more affluent cul-de-sacs of central Prince George’s.

The Harvard-educated lawyer and U.S. Army veteran spent 16 years in state government as a lawmaker and as deputy to Gov. Martin J. O’Malley (D) before finding himself out of public life and in debt in 2014.

Brown spent millions on an unsuccessful gubernatorial bid that ended with businessman Larry Hogan, a Republican, taking the governor’s mansion. Brown billed his congressional candidacy as his comeback, promising to learn from the mistakes of his earlier campaigns and connect with voters personally. But Brown started slowly, and his fundraising was weak as his competitors forged ahead.

Brown picked up the pace, hiring a campaign manager with a knack for field organizing. He crossed the district for waves, hand-shaking and meet-and-greets. In the waning weeks of the primary campaign, he invested $400,000 from his retirement fund in the campaign, despite owing about as much from his gubernatorial bid.

Brown’s lackluster start left an opening for a relatively unknown state lawmaker to compete. Peña-Melnyk built momentum throughout the contest, earning endorsements from national progressive groups and keeping pace with Ivey’s strong fundraising. She posted her best numbers a few weeks before the primary.

Peña-Melnyk, whose district includes northern Prince George’s County and parts of Anne Arundel, has spent nine years in Annapolis working on issues such as health care and education and by her count has passed more than 50 bills.