The Scottish government has admitted acting unlawfully while investigating sexual harassment claims against Alex Salmond.
Allegations against the former first minister, which he denies, were made to the Scottish government a year ago.
The government has now admitted it breached its own guidelines by appointing an investigating officer who had "prior involvement" in the case.
As a result, it conceded defeat in its legal fight with Mr Salmond.
Mr Salmond's case focused entirely on the fairness of the government's procedures and will have no bearing on a separate police inquiry into the allegations, which is still ongoing.
Speaking outside the Court of Session in Edinburgh, Mr Salmond said the case had resulted in "abject humiliation" for the Scottish government, which he led from 2007 until 2014.
He added: "The last time I was in that court was to be sworn in as first minister of Scotland. I never thought it possible that at any point I would be taking the Scottish government to court.
"Therefore while I am glad about the victory which has been achieved today, I am sad that it was necessary to take this action."
Mr Salmond also repeated his calls for the Scottish government's most senior civil servant, Leslie Evans, to consider her position, and warned that the case could cost the public purse £500,000.
And he thanked the 4,000 people who contributed more than £100,000 to a crowdfunding appeal to help pay for his legal challenge to the government's handling of the case.
With the government agreeing to pay his legal costs, he said the money will go to good causes in Scotland and elsewhere.
The government's admission that it had not followed the correct procedures came during a hearing at the Court of Session on Tuesday morning.
Judge Lord Pentland subsequently said that the government's actions had been "unlawful in respect that they were procedurally unfair" and had been "tainted with apparent bias".
The Scottish government's admission centred on an official it appointed to investigate the complaints against Mr Salmond, which were made by two women.
Its lawyer, Roddy Dunlop QC, told the court that the investigating officer was a "dedicated HR professional" who acted in good faith, but did have some contact with the complainers before being appointed to the case.
Mr Dunlop said this had led the government to accept there had been a "failure" in one aspect of the investigation, which could have given the impression that they were not acting impartially.
But he said the government did not accept a claim by Mr Salmond's legal team that the investigating officer had effectively been "assisting the complainers" and "giving them encouragement".
He also said there was "no question of an individual being held up as a sacrifice", and that the government had a "duty to investigate the serious complaints" that had been made.
Mr Salmond's successor as first minister, Nicola Sturgeon, told BBC Scotland that "in one particular aspect of the application of this procedure the Scottish government got it wrong", but that there was "no suggestion of any partiality in the process".
She also said it was "not my view" that Ms Evans, the Scottish government's permanent secretary, should resign.
Ms Sturgeon asked Ms Evans to draw up new procedures for handling sexual harassment claims, which the first minister signed-off, shortly before the complaints against Mr Salmond were made in January of last year.
The first minister said: "It is deeply regrettable that we are in the situation we are in today, not least for the complainants who had a right to expect that this process would be in every respect robust.
"I think the permanent secretary was absolutely right when these complaints came forward to subject them to an investigation and not to sweep them under the carpet because of the identity of the person complained about.
"That principle remains - the Scottish government, like any organisation has a duty when it gets things wrong to learn the lessons so that people who have complaints in the future feel confident in bringing them."
She later told the Scottish Parliament that she had met Mr Salmond three times after the complaints against him were made - at her home in Glasgow on 2 April, on 7 June ahead of the SNP conference in Aberdeen, and at her home on 7 July.
Ms Sturgeon added: "I also spoke with him on the telephone on 23 of April and 18 July. I have not spoken to Alex Salmond since 18 July.
"On 2 April he informed me about the complaints against him, which of course in line with the procedure the permanent secretary had not done, and he set out his various concerns about the process.
"In the other contacts he reiterated his concerns about the process and told me about proposals he was making to the Scottish government for mediation and arbitration.
"However I was always clear that I had no role in the process and I did not seek to intervene in it at any stage, nor indeed did I feel under any pressure to do so."
Analysis by Philip Sim, BBC Scotland political reporter
There were smiles and handshakes for Alex Salmond's legal team at the Court of Session, but it was a day of mixed feelings for the former first minister.
In the very room where he was sworn into Scotland's highest office, Mr Salmond found himself taking legal action against the government he once led.
Addressing the media outside, the former SNP leader said he was at once "delighted" to have won, but "sad" that it had come about in court.
And he turned almost every question to the future of the government's permanent secretary, Leslie Evans, who he says is responsible for the "abject surrender".
He was careful not to direct any ire towards his successor, Nicola Sturgeon. She says she has full confidence in her most senior civil servant - and has insisted the government's processes are robust, despite the "deeply regrettable" failure to apply them properly in this case.
Away from the politics, this is the end of the judicial review, but it's by no means the end of the road.
A police investigation into the complaints against Mr Salmond continues, and has not been affected by this court case in any way.
And the government has confirmed that the complaints it received in January 2018 have not been withdrawn - so the option of re-investigating them remains on the table, once the police probe has run its course.
In a statement released immediately after the case was resolved, Ms Evans said she wanted to "apologise to all involved for the failure in the proper application of this one particular part of the procedure", and in particular the two complainers.
But she insisted: "There is nothing to suggest that the investigating officer did not conduct their duties in an impartial way.
"Unfortunately, the interactions with the complainants in advance of the complaints being made meant that the process was flawed, however impartially and fairly the investigating officer conducted the investigation."
Ms Evans stressed that it was "right and proper that these complaints were investigated", and that the "procedural flaw in the investigation does not have implications, one way or the other, for the substance of the complaints or the credibility of the complainers".
And she said it was open to the Scottish government to re-investigate the complaints, adding that "subject to the views of the complainants, it would be our intention to consider this".
But Ms Evans said this would "only be once ongoing police inquiries have concluded".
A four-day hearing on the case had been due to begin at the Court of Session in Edinburgh next week, but that will now not go ahead.
The allegations against Mr Salmond date back to 2013, when he was still first minister. He has described the claims as "patently ridiculous".
The former MSP and MP, who lost his Westminster seat in the 2017 general election, resigned from the SNP in August but said on Tuesday he wants to rejoin.