Tech-wise while it might be possible (and certainly on the later end of this) historically there wouldn't have been enough coin available to do such a thing.
For this to work, it's going to take intricate metal parts. So in building your world, you are going to have to take tech in a different direction. Toothed gears have been around for a very long time, and while the mechanisms for this CAN be very basic, (weights and levers instead of gears) it's not something that's going to be common unless you have more of an industrialized society.
That's why Vending machines weren't actually widespread until the 1800s. While it WAS mechanically possible, it wasn't practically viable, even though this is an old, old invention.
So why did it take so long? When the earliest mention of something like this was in in the first century A.D.? You asked how it would work, and as far as the mechanics, I think you've been answered nicely, so I am going to add how it would work on world-building and social level--as in problems you need to look at and solve though the mechanics are there.
It's like a lot of other inventions--seen as a novelty, not good enough to be practical.
You'd need a big audience and the right product. Vending machines are an automation. They are practical when you have product, an audience to sell to, but you don't have people. And in this time period, merchants wanted control over their business. This is going to be a little expensive to make, so it has to be worthwhile. In an age of artisans, when consumers wanted to touch, weigh and examine what they were getting, most products would not fit into this. If you're thinking food, consider that there are many, many laws on the books during this period about serving rotten or counterfeit food. So this was an issue of the day. The earliest vending machine had the right audience and the right product. Holy water. It's water, so no need for quality control as far as a Medieval person is concerned, and it's trusted because it's sanctioned by the church. They've got the audience because the faithful are flocking in (church tourism was a big, big thing in Medieval times, so this is the one place it WOULD absolutely work.)
Preventing Theft. Having your machine inside a church might prevent theft. Leaving something full of money unattended, especially during more desperate times (plague, famine) is quite idiotic. An attendee might be a good idea. Theft from vending isn't such a big deal in a society that isn't starving.
A Culture Shift Take a look at this answer on how Medieval shops worked. Notice that this sort of hands on selling was ingrained in Medieval culture. I've linked the answer to dispel any notion you might have that stores worked back then the way that stores work now. There is no "browsing" as we know it. There is no wandering through a store.
A widespread, consistent monetary system, in low demonination This, alas, is something that wasn't present through much of the time period you are considering. So, when you build your world, you might want to look more towards the Renaissance than Medieval times. And actually, what most people think of as Medieval is actually Renaissance. (Rome also did pretty well with that, but not great. They liked changing up coins in celebration of events). You want everyone to have coin, you want that coin to be consistent and difficult to counterfeit, so that your machine can determine the weight and size of the coin. Even in Rome and even in the early Renaissance, the sort of consistent shaped coins that a machine like this would need...those are in short supply. I can actually see people having to buy a slug from a merchant to use in the machine just to take care of this.
Novelty vs. widespread Widespread is a real shift in culture away from the Medieval times as it is historically known. Vending machines became widespread because there was a NEED FOR THEM. So you are going to have to build a reason why as you worldbuild. Not simply because you want to stick them in. But because they fulfill an actual need and solve a problem. What that need is, and what problem they solve is up to you. If they are a novelty or just in one place, that's a whole different thing, and might be an attraction for a specific business to market a product--people might want to buy from them not just because they want the product but because they want to see the mechanism work. And if it is a novelty, the elements might even be exposed so people can see them work, which will mean you will still need an attendant...