Hard to tell from the photo, but they may be a type of sawfly. The larvae often look like a caterpillar with a different colored head on one end.
I think the ones in your leaf mould may have been looking for a soil-like material in which to shelter and pupate.