Fence gates can work. They allow you to get in and out but villagers can't use them (at least not last time I checked). You could replace the doors in houses with that, as long you can corral the villagers into buildings (which works best at night, when they already want to go inside). The ones already in the caves are probably lost causes, sorry.
Edit: One got lost in a cave and another in a mineshaft before i could seal it off. At least i got watermelon :3
PROUD USER OF THE STEVE SKIN