The mayor is the leader of a city. He or she heads the city government. Just as a president leads a country, a mayor leads a city. Like other politicians, mayors must run for office and win an election.
The word mayor comes from the Old French maire for "head of a city or town government" but most mayors probably like the original meaning, which is "greater, superior." Alas, mayors do run cities but calling them all superior is a stretch! In the United States, the president leads the country, a governor leads a state, and a mayor leads a city. If it’s used as a title for a particular mayor, as in Mayor Smith, it’s capitalized.