Hold it in place as you wind. If it still pops out you need to re-slot the nut. This would likely happen if you were replacing the original strings with larger ones.
It’s hard to tell because the picture is so small, but it looks like you might be winding in the wrong direction.
^^both of those things. And yeah, it does look like your wrap is going around the outside of the peg. You want it on the inside.