It came down to a sprint finish between the two men, with both knowing victory would give them the title, but the Spaniard was the stronger in the final metres. The world title is decided over a series of races and the Olympic champion Alistair Brownlee led the way in the standings after three victories but an ankle injury that has troubled him for much of the year kept him out of contention.
Going into transition at the start of the 10-kilometre run, the 25-year-old was grimacing in pain, and he eventually trailed in 52nd at the same venue where he won gold last summer.
Only Gomez has managed to rival the Brownlees since Alistair won the world title for the first time in 2009, with the elder brother winning again in 2011 and Jonny taking the crown for the first time last season.
Jonny, 23, said: "I'm a bit gutted because it was so close. When it's like that you ask yourself, 'What could I have done differently'? I knew that Alistair was struggling going into it. He's literally done four weeks of training the whole year. He did two weeks of training in St Moritz and was already as fit as me and then he hurt himself in Stockholm. The only reason he did well in Stockholm and Hamburg and Kitzbuhel is because he's so good. It's incredible what he's done this year. He shouldn't have raced. After Stockholm he really hasn't run. He ran on Thursday for the first time in about two weeks."
Jonny Brownlee and Gomez were locked together for the entirety of the run before Brownlee made a move with around 200 metres to go. Initially it looked like it might be enough but the Spaniard fought his way past to clinch victory and his third world championship.
Alistair Brownlee was critical of his brother: "I was watching Jonny and I was getting so frustrated with him. All he had to do was sit behind Javier until the last 50 metres and he was going to win. I saw him make his move with about 200m to go, and I thought, 'Oh no, what an idiot'. I'll be giving him a lot of stick for that. He's thrown a world title away through being a complete tactical numpty."