To my ear, the 'the' makes it a position and the absence of 'the' makes it a title.
Ever since graduating from high school, Ed Koch had wanted to be the mayor of New York.
In 198? (I only have a vague memory, from the live Simon and Garfunkel record ) Ed Koch was elected Mayor of New York.
Student or Learner