Events resulting in the formation of a multilayered cellular sheath surrounding an invader and thus preventing its development. This defense mechanism is often seen in insects in response to nematodes or parasitoids, which are too large to be phagocytosed by individual hemocytes. In some organisms the capsule is blackened due to melanization.