Ed Miliband has seized the Labour leadership in a dramatic, knife-edge victory that left his elder brother David's political dreams in tatters.

The younger Miliband, who ran a left-leaning campaign and only emerged as favourite in the last 24 hours, declared himself proud and elated as he pledged to reunite the party and put it back on the road to power.

But his victory was not without controversy as he won by a narrow margin – 50.65% of the vote to 49.35% for his brother – and thanks in large part to a strong vote from the unions. David Miliband received stronger backing from MPs and MEPs and from party members.

MPs who supported David Miliband warned that Ed Miliband's reliance on the union vote was a "disaster" for the party – leaving it open to charges that its leader would be in the pocket of its leftwing paymasters, and wide open to attack from the Tories and rightwing elements in the media.

Amid emotional scenes, the two brothers, whose battle had at times become fraught and bitter, hugged after the result was finally called in Ed's favour.

After 10 nail-biting minutes during which details of the votes were read out in stages with no single candidate reaching the required 50% until the fourth and final round, Ed Miliband was declared the winner.

The Ed Miliband camp erupted with joy as David, who smiled and applauded warmly, was left to contemplate how his lifelong ambition to lead his party and enter Number 10 as prime minister had been thwarted by his younger brother.

Struggling to maintain his composure, Ed, the 40-year-old former energy secretary, made a short, dignified acceptance speech in which he heaped praise on his brother and the other defeated candidates, Ed Balls, Andy Burnham and Diane Abbott. To David Miliband, he expressed his "extraordinary respect" and praised his campaign.

Ed Miliband said he had not imagined "in my wildest dreams" that he would lead the party when he joined aged 17.

He declared he would fight every minute of leadership for a "more prosperous, more equal, more fair society" as Labour seeks a route back to government. "Today," he declared, "the work of a new generation begins."

Most of the younger Miliband's campaign message had been directed towards those on the left of the party, declaring New Labour to be "dead" and attacking the drift to what he described as "brutish US-style capitalism".

By contrast David Miliband, who for years had been seen as a natural successor to Tony Blair, stuck resolutely to a position more on the centre ground, refusing to abandon the New Labour approach of appealing both to middle class and core Labour voters.

After the result was announced, former home secretary David Blunkett said: "We've never seen anything quite like what we've had, with two brothers neck and neck. These are brothers. They're blood brothers. They can't afford to fall out in the way we had with Tony and Gordon, and neither can we."

He said he believed Ed Miliband had won the election in the last few weeks. "If the election had been at the end of July, I think David would have won," Blunkett added. "That takes courage and it takes tenacity."

Leaders of the major unions welcomed the outcome. Tony Woodley of the Unite union said: "This is a fantastic achievement for him and for the policies he has been promoting.

"His victory, coming from nowhere a few months ago, is a clear sign that the party wants change."

But one senior MP said it was bleak day. "I think this will trigger a constitutional crisis in the party. It is complete madness that we can be seen to have a leader who was put there by the unions," the MP said.

Burnham said: "This election has been good for the Labour party – it has been conducted in a constructive spirit and thousands of members have been able to take an active part of the debate. Ed Miliband is a worthy winner."

Diane Abbott, the candidate of the hard left, was the first to drop out of the contest, followed by Andy Burnham and Ed Balls. Then it was down to a head-to-head between the two brothers. Attention will now turn to the leader's speech to the conference on Tuesday.

Then in two weeks' time the new leader will announce the composition of his shadow cabinet. Top jobs are certain to be given to David, if he agrees to serve, and Ed Balls, currently the shadow schools secretary.

As he left his London home to travel to Manchester for the declaration, David Miliband said speculation that his brother had won should be taken with "a very large skip of salt". But he insisted he could have work under Ed's leadership, joking that they would enjoy "more than a pint" whatever the result.