Per Brown University, early modern cities began awarding ceremonial keys to non-citizens as a special honor. In theory, if you had a city's key you could come and go as you pleased, even by night, when the city's gate was customarily locked. The point was to show that you'd earned the city's trust and affection. This is sometimes called the "freedom of the city."
Modern, gate-less cities still offer ceremonial keys. When Michael Bloomberg was mayor of New York City, he gave out at least 30 keys, including one to the Dalai Lama. Other recipients have been somewhat more controversial — just ask the haters of Alex Rodriguez, an athlete who also got a key to New York from Mayor Bloomberg.
The freedom of the city is a rather poor indicator of one's moral worth. In the photo above, the polarizing president of Brazil, Jair Bolsonaro, brandishes the key to the town of Pariquera-Acu, which he received at the opening of a bridge there. And in 1980, as Mental Floss notes, the city of Detroit granted a key to Saddam Hussein, in thanks for endowing a church.
ncG1vNJzZmivp6x7qL7Up56eZpOkunCDlmxwcmlfrLWiwIydpp6rXZZ6rLHYZquoZaSdsm6vyK2wZpmTqcKiuMuyZKiolaN8