...restore a positive self-image.
When someone has a poor self-image, they'll function subconsciously to displace their emotional pain, unfortunately, at times, at the expense of others.
An instantaneously gratifying way to numb painful feelings is to project it onto others, of which prejudice is one form.
When you judge another, you redirect your suffering away from yourself and towards the other. While only a temporary escape from your wound, it's a break nonetheless.
Inevitably, the pain has a boomerang-like quality and makes its way back to its origin, and so the protective behavior continues.
Continues unless, of course, one is aware of the physics of emotions, and appreciates that only love received can heal their fears.
"At the day of judgment, we shall all meet again." George Whitefield
Check out this study linking self-image to prejudiced behavior...
Fein and Spencer (1997) conducted a study on Self-image Maintenance and Discriminatory Behavior. This study showed evidence that increased prejudice can result from a person's need to redeem a threatened positive perception of the self. The study aimed to test whether a particular threat to the self would instigate increased stereotyping and lead to actual discriminatory behavior or tendencies towards a member of a "negatively" stereotyped group. The study began when Fein and Spencer gave participants an ostensible test of intelligence. Some of them received negative feedback, and others, positive and supportive feedback. In the second half of the experiment, the participants were asked to evaluate another person who either belonged to a negatively stereotyped group, or one who did not. The results of the experiment showed that the participants who had previously received unfavorable comments on their test, evaluated the target of the negatively stereotyped group in a more antagonistic or opposing way, than the participants who were given excellent reports on their intelligence test. They suggested that the negative feedback on the test threatened the participants' self-image, and they evaluated the target in a more negative manner, all in efforts to restore their own self-esteem.