US Congressman Joe Kennedy has been defeated in his bid for a US Senate seat, the first time the American political dynasty lost a statewide election in Massachusetts.

Incumbent Senator Ed Markey, a political veteran who recast himself as the fiery liberal in the race, harnessed the state's progressive energy to turn back a challenge from Joseph Kennedy III, the grandnephew of assassinated president John F Kennedy.

Mr Kennedy, 39, said he "called Senator Markey to congratulate him and to pledge my support to him and his campaign in the months ahead".

With votes still being counted, Mr Markey was leading 53.5% to 46.5%.

"Obviously, these results are not the ones we were hoping for," Mr Kennedy said.

Since he is not allowed to be on the ballot as a candidate for both the House and Senate, Mr Kennedy is not running for re-election to his House seat. Come January, there will be no one from the Kennedy clan in elected office. 

The grandson of Robert F Kennedy is part of the family's fourth generation to carry the political torch, entering the House of Representatives in 2013.

After his loss he told supporters that together they "built a campaign for working folks of every colour and creed, who carry the economic injustice of this country on their backs".

At 74, his rival is a generation apart, a progressive political workhorse who spent 37 years in the House before rising to the Senate in 2013.

For the Kennedys, there was much on the line. For all but two years since 1947 - nearly a third of the history of the United States - a Kennedy has served in elective office. 

Like his presidential great-uncle, Joe Kennedy III ran on the promise of a new generation. 

And he had received the rare endorsement of House Speaker Nancy Pelosi, who in 2018 appointed him to deliver the Democratic response to President Donald Trump's State of the Union speech.

Mr Markey, who framed his candidacy on a liberal, anti-establishment platform, has powerful supporters too.

They include Senator Elizabeth Warren and Congresswoman Alexandria Ocasio-Cortez, with whom Mr Markey co-authored the Green New Deal on tackling climate change.

Mr Markey is virtually assured victory in November's general election; only two Republicans have won US Senate seats from Massachusetts in the last half century.

The Democratic National Convention last month hinted that the Kennedys may indeed have a political future, when it aired a video message from JFK's daughter Caroline Kennedy and her 27-year-old son Jack Kennedy Schlossberg.

"We can reach these new frontiers, but only with a president who asks what he can do for our country," Mr Schlossberg said, echoing his grandfather's 1961 acceptance speech when he famously said: "Ask not what your country can do for you."