Well, Rama got the momentum on his 1st term and got a head start on his campaign for a 2nd term. The mayor was attacking Tomas on all fronts from the SRP scandal to his health problems. While Tomas' lacklustre performance in the Congress didn't get enough hype to counter Rama's campaign.
Tomas didn't lose because he was a terrible mayor.