Contagious yawning comes from our ability to infer and empathize with the thoughts of others in the theory of mind.
So, when we see or hear others yawn, our brain automatically triggers a neural mechanism response of primitive self-awareness and empathy modeling, and our yawn also happens.
The secret of yawning being infected for so many years is finally known!