(Bloomberg) -- (Bloomberg) -- Senator Ed Markey of Massachusetts defeated Representative Joe Kennedy in the state’s hotly contested Democratic Senate primary, besting the scion of a political dynasty by galvanizing the party’s progressive wing.

It’s a come-from-behind win for Markey, who is all but certain of a second full term in the November election in the solidly blue state. He trailed Kennedy after the 39-year-old grandson of Robert F. Kennedy Jr. announced his challenge in the fall of 2019; some had expected him to simply retire. But he campaigned aggressively as a liberal insurgent, out-raised his opponent and won a raft of endorsements, including from outspoken progressive Alexandria Ocasio-Cortez.

The results make Kennedy, who trailed in polls in the days leading up to the Bay State primary, the first member of his storied family to lose a political race in Massachusetts. Polls showed Kennedy had stronger support among minority and working-class voters, but struggled to win over wealthier, educated white voters and younger liberals.

In another race pitting a Pelosi-backed candidate against a contender supported by Ocasio-Cortez, House Ways and Means Chairman Richard Neal defeated Holyoke Mayor Alex Morse. Neal will run for re-election in November in the solidly Democratic 1st Congressional District in the central and western portion of the state with no Republican opponent.Pelosi had put her clout behind Neal to help him fend off a 31-year-old progressive challenger who was seeking to replicate the success of three other insurgents who ousted incumbent Democrats in earlier primaries this year.

Neal, 71, first elected to Congress in 1988, took the lesson from those earlier contests and campaigned hard to keep his seat. In addition to the backing of Pelosi and other influential Democratic Party leaders, he had an unusual cross-party endorsement from the state’s popular Republican governor, Charlie Baker.

The Senate contest divided Democratic leaders in the Massachusetts and in Washington. House Speaker Nancy Pelosi endorsed Kennedy, while Senate Minority Leader Chuck Schumer backed Markey. Markey’s co-sponsorship of the Green New Deal with Representative Ocasio-Cortez was central to her decision to support him.

The drive by Markey, 74, to run as an insurgent was a remarkable choice for a lawmaker who had served in the House from 1976 to 2013 before he ran for the Senate seat vacated by John Kerry when Kerry was confirmed as President Barack Obama’s secretary of state. Kennedy, wasn’t even born when Markey first ran for Congress.

Markey’s victory comes after candidates backed by progressive groups ousted three Democratic incumbents in this year’s primaries: Foreign Affairs Committee Chairman Eliot Engel in New York, Representative William Lacy Clay of Missouri and Representative Dan Lipinski of Illinois.

Massachusetts voters had a choice between two candidates with liberal voting records. Markey and Kennedy agree on many policies, including the Green New Deal to combat greenhouse-gas emissions, a single-payer health care system and overhauling policing practices in the U.S. In August debates, they argued more about outside spending in the race and the increasingly bitter tone of the contest.

Markey called Kennedy at one debate “a progressive in name only.”

In his advertising, Kennedy accused Markey of forgetting about the working-class residents of his home town of Malden and focusing more Washington than Massachusetts. One recent spot featured footage of late President John F. Kennedy and his grandfather, the former attorney general. He said that he has the fight for racial justice “in his blood.”

Kennedy’s political future is now uncertain as he faces his congressional exit. The Boston Herald reported earlier this week he didn’t plan to run as a write-in candidate for his House seat.

©2020 Bloomberg L.P.