Ed Miliband has beaten his older brother David to become the new leader of the Labour party.

He took the crown on the fourth round of a complex voting system, due largely to strong support from the trade unions.

The knife-edge result, announced in Manchester on the eve of the Labour party conference, drew to a close an intense four-month leadership contest between five party rivals. The result defied the odds, which until this week had been in David Miliband's favour.

Ed Miliband won by a wafer-thin margin of just 1.3 percentage points after second preference votes were taken into account. David Miliband secured 37.78% of first preference votes, compared to 34.33% for his brother.

But Ed Miliband won with 50.7% of votes to David Miliband's 49.3%, after the second preference votes of Diane Abbott, Andy Burnham and Ed Balls were taken into account.

Abbott was first to be knocked out, followed by Burnham and then Balls.

Ed Miliband, the MP for Doncaster North, who was first elected to parliament in 2005, and was not initially expected to stand against his brother, told a packed conference that it was "an amazing honour" to have been elected leader.

"I joined the party when I was 17 and never in my wildest imagination did I believe I would one day be party leader. You have put our trust in me and I am determined to pay that trust back to you."

Ed Miliband promised to unite the party as it faces opposition in the face of coalition cuts. "I know we lost trust, I know we lost touch, I know we need to change," he told the conference as he signalled a new political generation at the party helm.

He began his victory acceptance speech by addressing each candidate in turn, beginning with his older brother.

Ed Miliband said: "David, I love you so much as a brother and I have so much respect for the campaign that you ran. You taught us an important lesson which is that we can be a party that reaches out to the community and we can be a serious party in government again."

David Miliband must now decide whether to serve in the shadow cabinet under his younger brother. Defeat will come as a blow: he has long harboured ambitions to lead the party.

Prior to the result, both insisted their relationship could withstand the rough and tumble of the leadership race, though negative briefings by the two camps began to suggest the pressure of being the two front-runners was beginning to show.

Doubts surfaced about whether David Miliband, who had long been talked of as future leader by the Blairite wing of the party, would realise his potential after failing to challenge Gordon Brown for the leadership in 2007 and again, in 2009.

On the later occasion, he decided not to follow his friend James Purnell, who quit the cabinet in protest at Brown's leadership.

After Brown stood down, David Miliband made clear his determination by being the first to declare his intention to stand, before Ed Miliband added his name to the list of contenders a week later and cast himself as an Obama-style "change" candidate.

Ed Miliband built up a support base as the summer progressed, and secured the high-profile backing of the country's three largest trade unions – Unite, Unison, and the GMB.

Earlier this week, it emerged that senior advisers to the two Miliband camps had held a secret planning meeting at which they discussed what role each might play in each other's cabinet when one of them was declared today.

The leadership of Ed Miliband is likely to be defined by his approach to the deficit. Harriet Harman, who has been acting leader since Brown stood down, has suggested that Labour's approach to the deficit needs to be reviewed.

Balls has won over many critics with his argument that Labour's plans to halve the deficit within four years were a mistake and not deliverable. The shadow education secretary, who previously served as a Treasury adviser to Brown, argues that the economic climate means the party must set out a longer timetable for reducing the deficit.