Ken Livingstone has won a High Court appeal against a finding that he brought his office into disrepute over comments made to a Jewish journalist.
Ken Livingstone likened the journalist to a Nazi camp guard
The mayor was found guilty of being "insensitive and offensive" to Evening Standard reporter Oliver Finegold by the Adjudication Panel for England.
But Mr Justice Collins said the panel had misdirected itself.
The judge said that while the mayor should have apologised he had the right to freedom of speech.
Mr Livingstone, who was facing bankruptcy should he have lost the case, was awarded his full legal costs which were thought to be about £250,000.
After the hearing, Mr Livingstone praised the judge's "incredibly sensible judgment".
"It brings a lot of common sense to approaching the balance between freedom of speech and the various roles of the authorities in all of this," he said.
The Evening Standard issued a statement saying the whole costly affair could easily have been resolved had "Mr Livingstone simply apologised for his remarks at the time".
The mayor had likened Mr Finegold to a Nazi camp guard as he left an event at City Hall in February.
At the hearing on Thursday, Mr Justice Collins described the mayor's comments as "unnecessarily offensive" and "indefensible".
But he stressed that the mayor had the right to freedom of speech in a democratic society and to express his views within the law "as forcibly as he thought fit".
"Surprising as it may perhaps appear to some, the right of freedom of speech does extend to abuse," he said.
The judge said although the panel had been entitled to conclude the Nazi jibe brought the mayor into disrepute, he was not persuaded the office had also been brought into disrepute.
He said that the panel had applied a test which "failed to recognise the real distinction between the man and the office".
The decision to suspend the mayor for four weeks over the comments was also "clearly wrong", he added.
But Mr Justice Collins stressed the judgement did not indicate he thought the comments were appropriate.
"They clearly were not. His initial question, 'Were you a German war criminal?', was obviously intemperate.
"He should have realised it would not only give great offence to him, but was likely to be regarded as an entirely inappropriate observation by Jews in general, and those who had survived the Holocaust in particular," he said.