The mayor of a city or town would basically *never* come from the aristocracy.
Here is the thing. The king (or whoever it was that held the territory) struck a deal with the people in the city. He granted them a town charter. In contrast to the rural areas, where the aristocratic lords could basically order anything they wanted, the city had now the right to govern itself in a way it liked, without interference (not open one at least) from the lord.
Picture: town charter of the German town of Lübeck – Lübeck was among the very first chartered towns in Germany, and subsequent charters would for example be referred to as “chartered according to Lübeck law”. The Lübeck charter was granted in 1160, the document above describes the Lübeck law in a version written in 1294.
Now, why would the lord do that and give up control? Because of money. Granting a settlement a charter was a great way for income. Immediately, because the charter would be paid for by the citizens, and for the future because the king retained the right to tax the town – and experience had shown that an independently governed city would provide more taxes than a similar settlement directly controlled by the lord. Why? Because the rural areas were all about agriculture, and that was something that the aristocracy knew about. Urban settlements in contrast would grow rich through trade. And therefore, the government of the city would first and foremost look for setting conditions in the city in a way that the city could trade more, and thus become more rich. That needs a certain skillset, and that skillset was naturally available to the persons that walked the walk already – the merchants in the city.
So far, so good. Now, the way how a city determined its government varied from city to city, or rather from charter to charter. For example, in London the city council alone would elect between different candidates for the mayor. In other cities, all burghers or freemen would have the right to cast their vote in an election. Not everybody living in a city would be a burgher though. This was usually reserved for people who belonged to a guild. The most powerful guild would usually (but not necessarily) be the merchants guild, but basically all craftsmen would as well be organized in guilds – potters, cobblers, smiths, butchers, tailors, the list can be very long. Guilds were a ubiquitous, extremely important element in medieval cities.
Picture: some important guilds with their “heraldic” symbols; in German, it reads from upper left to lower right “Painter, Barber, Carpenter, Cooper, Brewer, Bookbinder, Roofer, Soapboiler, Wheelwright, Fisher”
The councilmen and the mayors would universally be important – and rich – persons from one of the guilds. As I said, most often it was a member of the merchants guild who would get the job.
And no member of the aristrocracy would ever sink so low as to apply for membership in a guild.
All this being said … sometimes, and actually not that rarely, in bigger cities it was possible for a “hereditary dynasty” of councilmen to develop. Staying with my Lübeck example, the Pleskow family, a rich merchant family in Lübeck and Visby, had family members in the Lübeck city council uninterrupted from 1299 to 1451. In a way, the merchants would be a city’s aristocracy.