It should be possible to keep animals alive that way, as they don't actually have to eat. However, animals can teleport in (spawn inside complete enclosures and on isolated islands), so they can presumably teleport out as well, eventually.
One case of teleporting in was a squirrel who appeared on a tree inside my "stable". The "stable" has a wall with a closed door and floor/roof over all tiles except two trees. The reason I've left the trees there is that it allows me to tie the cow and the sheep to one tree each, so I don't have to jump through hoops to prevent them from escaping when entering/exiting (a hook on the wall had been better, but the game doesn't support that). I eventually killed the squirrel, so I didn't wait to see if it would disappear.
I've also had elks spawn inside the trap fence protecting my farm plots (finding an elk in a trap in the fence, with tracks only on the inside, and none on the outside).