It's a psychological thing
Much the same thing that happens when you think "Man, I'd better not forget to do that" then the next thing you know "AWW SHEIT! I forgot!"
What happens is that when you think about what you don't want to do (forget to do that), a visual appears in your mind of you forgetting it, or the results of you forgetting it, and subconsciously, your mind tries to figure out ways to make it happen.
When you see or hear someone yawning, you imagine sleepyness/wakingness/some other change of body mode (like going from being in an athletic mode to sitting down and watching TV or something), and so without even thinking about it, you yawn too (or get the urge to yawn) because that is what you were visualizing inside your mind.
Funny I should know all this stuff about minds, when mine is all scrambled and odd