There are two functions that I'm aware of that are involved, though there of course may be more I'm simply unaware of.
The first is so-called mirror neurons. Cognitively, neurologically, and evolutionary, we generally assume that the brain tends to be very efficient to be able to get its immense job done in so little space, so quickly, with so little 'power' (food). One result of this pressure is re-use, so that the same parts of the brain that are used in active experience of something are also used for recalling, thinking about, and indeed perceiving others experience similar things (thus the mirror neurons). To see someone else itch compels us to think about itching, and to recall/think about/understand itching is to activate the same parts of the brain that register the concept of itching in ourselves.
This then neatly leads into (and overlaps) with another set of effects: conscious awareness and selective attending. The nature of our conscious awareness is that it is limited - we can't consciously attend to everything at once. And thus we have perceptual and cognitive filters (and we have lots of theories over time that have developed this context - I think the "leaky filter" hybrid model being the most recent, but I can't recall the name of the researcher(s) who came up with it).
This set of functions give us the cocktail party effect and the invisible gorilla experiments, change blindness, and so many fun others. It also effects how we respond to "how happy are you" questions; another fun experiment was that finding a nickle on a copying machine before you get asked about your happiness and satisfaction in life causes one to respond more positively to even general questions.
But I digress...
In contagious yawning - and in itching! - the act of perceiving someone itching can direct our attention to personal comfort and any potential itchiness. For instance, if you are fully dressed right now (including socks): you know that 'seam' at the end of the sock down by your toes? Does that always go above your toes on the top of your foot, or does it sometimes kind of work it's way down so it's at the bend of your toes underneath your foot?
Well, I have gone all morning without thinking about it, but now I realize my sock is a little twisted, I think the heel of the sock has slid a bit off-center, and the top of my boot is rather tight while the lower part of the laces could be tighter. Also, my ankle kind of itches a little.
The fact of modern life is, quite frankly, there is always something that could use a good scratching! Like my back right now...answering this question is making me wish for a scratching post. Multiple layers of clothes of varying fibers, little skin folds, body hair, all those tiny little follicles and pores mean there's bound to be at least one little pimple somewhere, and even if you are very flexible there's always places that aren't terribly easy to reach...
So perhaps a better question is "why aren't we itchy all the time?!" Thankfully, most of us have filters that consider many of these sensations relatively unimportant, so they don't get passed over into something that captures our conscious awareness. Also, we are busy: there's lots to think about in the world, so we tend to only think about it when we are given a cue like trying on clothing, or looking at an irritated/injured piece of skin...or seeing someone else itching.
Given these are cognitive processes, we can also view what happens when they go wrong, as manifested in symptoms and disorders such as intrusive thoughts, "sensitive skin" or tactile sensitivity (especially common in the autism spectrum), etc.
First we perceive, then we imagine, then we experience - empathy! Sad songs can make us sad, in part because they remind us of the sadness that is an unavoidable part of life, and also in part because to think about sadness we can't help but feel at least a little sad; the only reason we aren't sad all the time is because there are happy things to think about, too.
Itching is like an elephant - try not to think about it. Oops, too late!