Media playback is unsupported on your device Media caption Ed Miliband: "You have put your trust in me and I am determined to repay that trust"

Ed Miliband has won the Labour leadership after narrowly beating brother David in a dramatic run-off vote ahead of the party's conference.

Ed won by just over 1% from former foreign secretary David after second, third and fourth preference votes came into play.

He said a "new generation" had taken charge of Labour and it had to change.

Ed Balls was third, Andy Burnham fourth and Diane Abbott last in the ballot of MPs, members and trade unionists.

Mr Miliband, 40, replaces acting leader Harriet Harman in the contest triggered by the resignation of Gordon Brown.

Winning line

The former energy secretary appears to have benefited from a last-minute surge of support before voting in the postal ballot closed on Wednesday.

HOW ED MILIBAND WON Round 1: David Miliband 37.78%, Ed Miliband 34.33% Diane Abbott eliminated

Round 2: David Miliband 38.89%, Ed Miliband 37.47%. Andy Burnham eliminated

Round 3: David Miliband 42.72%, Ed Miliband 41.26%, Ed Balls eliminated

Round 4: David Miliband 49.35%, Ed Miliband 50.65%. Ed Miliband wins. In quotes: Reaction Full results: Round by round Nick Robinson: Hurdles ahead

Older brother David won a majority of support from Labour's MPs at Westminster and party members, but Ed was ahead among members of trade unions and affiliated organisations in Labour's electoral college voting system.

BBC political editor Nick Robinson said in the first three rounds of voting David Miliband was ahead - it was only when votes were reallocated as the other candidates were knocked out that his younger brother was pushed over the winning line.

Mr Miliband hugged David after the result was announced.

In his victory speech, he vowed to unify the party, telling delegates: "The Labour Party in the future must be a vehicle that doesn't just attract thousands of young people but tens of thousands, hundreds of thousands of young people who see us as their voice in British politics today."

He paid tribute to his predecessors Gordon Brown and Tony Blair, but added: "We lost the election and we lost it badly. My message to the country is this: I know we lost trust, I know we lost touch, I know we need to change.

"Today a new generation has taken charge of Labour, a new generation that understands the call of change."

'Brilliant campaign'

Mr Miliband received a standing ovation from delegates as he made his way from the hall, with his brother David at his side.

Former minister Tessa Jowell told BBC News David Miliband's defeat will be a "moment of tremendous pain and disappointment" for him.

But the former foreign secretary was doing his best to put a brave face on it, telling BBC News: "This is Ed's day, it's a big day for the Miliband family, not quite the day for the Miliband family that I would have wanted - the Milband D family, rather than the Miliband E - but that's the way things go."

He said the party now had to rally behind his brother and there was a "strong mood" within Labour to do so.

He refused to talk about his own future, amid speculation about whether he would serve under Ed.

Ed's former Treasury colleague Ed Balls paid tribute to his "brilliant campaign" adding: "It's a hugely important moment for the Labour Party, now we have got to come together."

He said Mr Miliband had to be given "the time and space to get this right".

Diane Abbott, the most left wing of the five said Mr Miliband "will make a fantastic leader".

'Cut the deficit'

Mr Miliband singled out Ed Balls for praise in his victory speech, leading to instant speculation that the shadow schools secretary could be in line for a top job after next month's shadow cabinet elections.

Conservative Party chairman Baroness Warsi congratulated Mr Miliband on becoming leader of the opposition, but she told BBC News he owed his victory to votes of trade unionists, which she feared would lead to an "abandonment of the centre ground" by Labour.

She said it was now time for Mr Miliband to "to tell us how he'd cut the deficit".

Prime Minister David Cameron called Mr Miliband from his Chequers country retreat to congratulate him on his victory.

Media playback is unsupported on your device Media caption David Miliband: "This is Ed's day, I am obviously genuinely delighted for him"

In a three-minute conversation, he told the new leader of the opposition that people would tell him that his was "the worst job in the world" but that it was not that bad and promised to keep him in touch with matters of national security.

Ed Miliband responded by saying that he would lead "a responsible opposition" which would work with the government where they could, according to the BBC's Nick Robinson.

Ed Miliband, who has been MP for Doncaster North since 2005 and was energy and climate change secretary until Labour's election defeat in May, is a former aide to Gordon Brown at the Treasury, who joined the Labour Party at the age of 17.

The son of the late Marxist intellectual Ralph Miliband, he is the 20th person to take on the leadership of the Labour Party.

He positioned himself to the left of his brother, the former foreign secretary who is five years older and who started the four-month contest as frontrunner.

He sold himself to party members as the "change" candidate, securing the backing of three of the four biggest trade unions - Unite, Unison and the GMB.

Under Labour's complex electoral system, voting power is divided equally between three sections: MPs and MEPs, affiliated organisations including trade unions and ordinary party members.

If no single candidate secures 50% or more of the first round vote, the last-placed contender is eliminated and the second preferences of their backers are redistributed.

The elimination process continues until one of the candidates reaches 50% or more, potentially ending, as in this case, as a head-to-head fight between two of them.

After four rounds of voting Ed Miliband won with 175,519 votes, while David Miliband received 147,220 votes.