The judgement entitles M&S to claim back £3.5m in overpaid VAT, bringing an end to the 12-year dispute.
Tony McClenaghan, head of indirect tax at Deloitte, which acted for M&S, said: "This is a sweet victory for M&S, for whom this was as much a matter of principle as about recovering the money."
While the UK tax authorities accepted that the high street retailer had wrongly overpaid tax on chocolate teacakes over a number of years, HM Revenue & Customs (HMRC) argued that repaying the full amount of the tax would "unjustly enrich" M&S because about 90% of the VAT had been passed on to customers.
M&S argued that the defence of unjust enrichment was applied to its valid claim for wrongly paid tax, but the same defence was not applied to block comparable claims made by its competitors.
McClenaghan added: "Common sense has prevailed. It is unreasonable for different retailers to be treated differently in relation to similar transactions. The fact that the UK authorities changed the law in 2005 suggested that comparable situations had previously been treated differently."