It was most likely random.
Idk, a lot of animals shed their skin and whatnot, like snakes, and most insects molt.
Quite possibly during a molting phase they just develop more and more capabilities; maggots turn into ants right, eggs into creatures?
That could be seen as a form of metamorphosis. Likely, when they get to a certain size, it was just more ideal to grow these characteristics, under a controlled setting, rather than just in the environment.
It could be seen as development outside the egg.