Its legs must have become less useful as the animal evolved over time.
"If something is not useful it can regress without any impact on the (animal's) survival, or regression can even be positive, as for here if the leg was disturbing a kind of locomotion, like for burrowing snakes or swimming snakes."
Thus during the process of evolution, they made adaptations that eventually let them loose their legs.
Remember that this was not a spontaneous proccess. It must have been happend so slowly.