Amid celebratory scenes in the high court from the fans who will now become its owners, Portsmouth Football Club has been saved from liquidation after an 11th hour deal between its administrators and the former owner of the club.

The out-of-court settlement, between administrators BDO and Balram Chainrai's Portpin, finally draws a line under a lengthy and at times bizarre saga of chronic mismanagement under a series of owners that resulted in the club collapsing into administration twice in two years.

Under the deal, which is understood to involve Chainrai accepting the £3m for Fratton Park that the Portsmouth Supporters Trust always insisted it was worth, as well as a six-figure sum to cover costs and compensation, the club will become the largest yet to be owned by its fans.

Ashley Brown, the chairman of the PST, which will become 51% shareholders in the club under the new structure, said it was a "historic day", adding: "We're incredibly pleased. It's been a draining fight, but we've achieved what we set out to do."

He said the club, virtually certain to go down to League Two once a 10 point penalty is imposed by the Football League when it exits administration, could now begin the long climb back up the divisions.

"We now want to look to the future. We've got a clean break and we've now got a football club that is owned by the fans, owned by people who love it. We're going to have a football club that is run sustainably, transparently and in the proper manner."

On a dramatic day in court 30 of the Rolls Building annexe of the high court, Justice Peter Smith twice adjourned proceedings so he could read new paperwork and was then told an out-of-court settlement had been reached. To his growing irritation, there was a lengthy delay while it was drawn up and it reached court shortly before 4pm.

When the judge finally said that the deal would leave the Trust in a position to take over the club, a beaming Iain McInnes, the local businessman who will become chairman, hugged everyone in the courtroom as Mick Williams, a Trust board member, wiped away a tear.

The Trust has been in pole position to buy the club since the Football League made clear that its bid was the only one it would approve and set a deadline of the end of April for its future to be resolved. But the deal was complicated by a last-ditch bid from a consortium represented by the football dealmaker Keith Harris, who offered £6.3m for the club.

He later resubmitted an offer purely for Fratton Park, without the club attached. Harris continued to insist that the Football League position remained unclear and that his bid offered the club a better chance of survival, but late on Tuesday Chainrai moved to negotiate a settlement with BDO.

It brings to an end the latest chapter of uncertainty over the future of the club, which became the first Premier League side to collapse into administration in 2010 with debts of £117m and has been in freefall since. The club's administrator, BDO's Trevor Birch, went to court to secure an order from a judge forcing Chainrai to sell the ground for £3m – despite a larger offer from a rival consortium led by Harris.

Chainrai held a charge over Fratton Park dating from the first administration. The Russian banker Vladimir Antonov, to whom he sold the club, was later charged by the Lithuanian prosecutor-general with bank fraud – a charge he denies – and the club went into administration for a second time in February last year.

Under the new ownership structure around 2,000 fans have pledged almost £2m, a significant proportion of which has been placed in Escrow. There are also a number of "presidents", who are wealthy fans who have each invested more than £50,000, raising around £1.5m that is also in Escrow. In addition, the property investor Stuart Robinson has purchased the land around Fratton Park and will loan £1.2m to the Trust towards the initial purchase of the ground.

Around £9m in parachute payments due from the Premier League will immediately be used to settle football creditors, while other creditors are expected to receive just a couple of pence in the pound.

Birch, a former professional footballer and the former chief executive at Chelsea, Everton and Leeds United, said that other clubs could now look to follow Portsmouth's example. Swansea City, in which fans have a 20% stake, have been held aloft recently as the "ideal" model of club ownership by Premier League chief executive, Richard Scudamore. Other fan-run clubs include Exeter City and AFC Wimbledon.

"For some clubs it's definitely a model to look at very carefully. For a club like this that has been through so much, it almost needs to return to the fans for a bit of TLC," said Birch. He said the club was "very close" to going bust throughout the 15 months when he had been in charge as administrator.

"In the early days, people got sick of me saying it might go into liquidation. And if it had and the stadium had been lost, it may never have reappeared."

As he spoke, the local businessmen and Trust members who will now be charged with not only getting Portsmouth back on to an even keel but become standard bearers for fan ownership burst into a chorus of the Pompey Chimes in front of the court.

"I shed some tears in court because it's a fantastic day for the football club. I always believed we'd do it," said McInnes.

"It's been my job to hold everyone together. It's about the long-suffering fans of Portsmouth Football Club who hopefully don't have to suffer any longer. What everyone underestimated from the word go was the togetherness that has been forged. We've won the war, now we have to win the peace."