This is the lock on the door of the Serranos Towers, which were one of the entrance gates to the city in medieval times. The towers were built at the end of the 14th century - no idea whether the lock is the original one, but it's clearly really old. Surprisingly, it still works. Although the doors are almost always open nowadays, sometimes they get locked (in celebrations, so they can open them again in a symbolic way), and they use this lock. Moreover, if someone is awarded the Key of the City, that key opens this lock.