Jeremy Corbyn's policy on Brexit has triumphed at Labour conference, as members endorsed his stance to stay neutral while negotiating a new deal.
The party voted against a motion which would have seen Labour backing Remain in any future referendum.
But there was confusion as the votes were called, as the chair of the proceedings faced calls for a recount.
Labour's position on Brexit has dominated the conference agenda, with huge disagreements over the issue.
The party's draft plan for its Brexit policy, put forward by Mr Corbyn, suggests that, if Labour wins power in a general election, it would remain neutral while negotiating a new deal with the EU within three months.
It would then hold a referendum within six months, and the party would decide which side to back ahead of that at a special conference.
Grassroots activists at the conference have been pushing for an unambiguous stance, tabling a motion calling for Labour to campaign "energetically" to Remain.
But this motion was rejected in a show of hands while a motion setting out the leadership's official position and another endorsing its handling of Brexit were overwhelmingly passed.
After the results were announced by trade union official Wendy Nichols, there were charged scenes in the conference hall.
Several delegates called for the votes to be counted individually, suggesting the outcome of the Remain motion was much closer than officials had suggested.
One delegate said there had to be an official card vote as "this is one of the most important decisions Labour is going to take in the next decade".
The result is a major boost for Jeremy Corbyn, who was backed by the majority of Labour's 12 affiliated unions, including Unite and the GMB.
Unison had broken ranks with other unions to back the Remain motion.
Shadow Brexit secretary Sir Keir Starmer said he was disappointed by the result of the vote, and that he would campaign for Remain.
"Would I have liked us to have gone a bit further and won that vote? Of course I would - but I don't want to take away from the fact that is quite considerable movement," he said.
The leader of the Unite union, Len McCluskey, said the vote showed ordinary members coming behind the Labour leader's stance in a show of loyalty.
"What you've seen here is a massive show of support for Jeremy Corbyn," he said, adding that it was "time to unite".
The vote was decisive - the Labour leadership position on Brexit triumphed.
Those calling for a more robust Remain stance at the likely snap election were defeated.
But the manner of the triumph was immediately called in to question.
The vote wasn't a secret ballot, it was a show of hands.
Calls for a card vote - where the vote of each delegate is individually counted in secret - were dismissed by the chair.
That's not to say there wasn't clear show of support for the leadership.
But some remainers maintain that the vote would at least have been closer if it wasn't conducted in public.
That's because the debate became - for some delegates - a demonstration of support for the leadership, close to an election, rather than a pure test of opinion on Brexit.
The conventional wisdom was that Jeremy Corbyn might have to rely on the big unions - with 50% of conference votes - to win.
But some unions chose to defy him, making a defeat possible.
In the end a section of the grassroots - the ordinary members - did not prioritise their own pro-Remain position and rallied round Jeremy Corbyn.
Remainers are now accentuating, for them, the positive - that the party is now unambiguously backing a new referendum, with Remain as an option.
Nonetheless, Labour will go in to the election unable to say whether it will officially back leave or remain in a subsequent referendum.
But after a difficult few days, most of those close to Jeremy Corbyn are relieved tonight, and some are jubilant.
Andrew Lewin, the founder of Remain Labour, said the vote represented the "grassroots against the party machine - and the machine won".
"If this fudge is the Labour policy at the next general election, we will drive Remain voters away."
Another campaigner, Michael Chessum, from Another Europe is Possible, said: "Labour members, 90% of whom want to stay in the EU, will be deeply disappointed with this decision."
But Labour MPs remain divided over the issue.
Speaking before the vote, shadow Treasury minister Annaliese Dodds said the economic consequences of Brexit were "so severe" that she believed Labour must back remain in another referendum.
"Is it going to be easy?" she told the BBC's Carolyn Quinn. "No it is not, because people are passionate in both directions."
But Stephen Kinnock, the MP for Aberavon, told a fringe meeting organised by the Social Market Foundation that Labour had had "more Brexit positions than the Karma Sutra".
Describing the first two days of conference as an "utter shambles", he said Labour should have stuck with its 2017 manifesto pledge to honour the referendum result and moving away from this this would not go down well in Leave constituencies.
"Our position on Brexit is being treated with ridicule on the doorsteps in my constituency," he said.
Away from Brexit, Labour has announced a pledge to introduce free personal care in England for over-65s, so they will not have to pay for help with dressing, washing and meals.
In his speech, Mr McDonnell also pledged to end in-work poverty within five years and to move to a four day, or 32-hour, working week within a decade without any cut to pay.
These are the latest of several new policies likely to feature in the party's next election manifesto, including pledges to:
- Scrap Ofsted, which it called "unfit for purpose", and replace it with a new schools inspections system
- Make NHS prescriptions free in England, bringing it in line with Scotland, Wales and Northern Ireland
- Force large employers to provide flexible working hours to women experiencing symptoms of the menopause