cocoon formation as part of
the clitellum is composed of thick skin covering mostly three or four segments in the first twenty segments of the earthworm.
it is highly glandular and is capable of secreting a very viscous jelly (the cocoon) which later becomes a very rigid and elastic band around the earthworm, making the worm uncomfortable, much like a tight ring on your finger.
now the clitellum starts secreting the cocoon after copulation, the mutual exchange of gametes between two worms (they are hermaphrodites, by the way).
the worm tries to wriggle itself out of the tight cocoon by crawling backwards, so now the cocoon passes from segment (say) 14 to segment 1 and then out completely.
note that the cocoon is very elastic and as it passes both sets of gonopores postero-anteriorly, squeezes the gametes out of their respective receptacles. after the cocoon leaves the worm, both ends of the cocoon snap shut, owing to their elasticity, giving the perfect environment for fertilization and development of baby worms. the worm may produce several cocoons after a single copulation.