Its called 'Post Hypnotic Suggestion', in the same way that a smoker, who hadn't even thought about lighting up, sees someone else smoking, then fancies one himself.
Another example is someone having a scratch, if they continue to do it, the odds are, that of you had been watching, before long, you'll be scratching too.
I don't think there is a definitive answer to this question.
As suggested in Wikipedia, the herd instinct may be relavent, a bonding gesture, or a silent signal of potential danger - showing fighting fangs to the rest of the tribe suggersting that a threat is imiment.
Another suggestion is that it is the body's way of abosribing extra oxygen when required, although since foetuses yawn in the womb, that rather dispels that notion.
Scientists will no doubt continue to debate the question, but I don;t belive anyone really knows for sure.