There were "free cities" in the Medieval world. Legally, these cities had self-government; they were technically within a larger kingdom, but were usually ruled by some gorup of notables under a mayor (London, I believe, had this legal status).
Most of these governments (if my rather shaky...