I can think of a couple of ways... You could start with a larva close to pupation (and so not needing much more food) getting picked up and nurtured by the ants for whatever reason (who quite possibly weren't as selective as they are now, as the was nothing previously trying to parasitse them in this way). Once that happens, it's obviously got a major survival advantage, so the trait would sweep through the rest of the population fairly quickly. After that, it's just a question of further refinement to the mimicry, probably involving some co-evolution with the ants, and starting to eat ant larvae by get picked up earlier in its life cycle.
Or you could start off with butterfly larvae moving onto diverse insect food sources, then developing adaptations to allow it to fool the ants, and then becoming specialised to eat only ants.
I suspect the real story is probably neither of those, but whatever it was, it only takes a very marginal advantage at each step to get strongly selected in a fast reproducing r-selected species.