Disclaimer: This post is purely based on speculation and a bit of observation; it possibly can be used to design creatures or similar for games, stories, and so on. That also means it doesn't necessarily need to be correct.
You know, "watching an organism grow is watching evolution in time lapse" is extremely interesting thought to play with. Especially in regards of caterpillars/
The first stage: Somehow maggot-like creatures learned to cover themselves in a coccon at some point in their life. Developing this kind of behavior may have been caused by severe environmental changes (kind of like a hibernation during dark times, such as enormous volcanic eruptions) or protection from predators. Maybe they spend their last days like this after mating, to shelter their offsprings, including themselves - then the early caterpillar would die after all offsprings were born.
The ability of caterpillars to evolve inside of their cocoon may have been developed after the first stage. It seems that these insects discovered resting in a cocoon as an effective way to survive, which may point to the fact they may have found a niche in the food chain. The interesting thing on this is to ask yourself what circumstances must have exist in order for this to happen; the causes for this may have worsening environmental changes, i.e. a cataclysmic event, a volcano dust covering up a majority of the atmosphere, or simply protection from the sun or predators, as mentioned before; Something must have happened to make these animals stay longer in their sheltering home than usual. Maybe these creatures started out with very short metamorphosis times (i.e. mating season) then the period was extended as they evolved. Maybe they could get out at any time as well, and basically "gathered" new abilities that were passed on to the next generation to be "applied" to the offspring while metamorphosis.