Encapsulation occurs primarily as a protective mechanism. The body may form a capsule around an area of infection or inflammation to prevent the spread of harmful agents to adjacent tissues. It can also occur around foreign bodies, such as splinters or implants, as the body's immune system attempts to isolate and contain the foreign material.