I've posted a question about it a long while ago, so I'll copy the chosen answer here:
"Why" is used as an expression to give the statement emphasis . "Why" can be replaced with something similar to "wow" in those examples.
Why, thank you! = Wow! Thank you!
"Why, yes. Didn't you know I was his cousin?"
- In this case, "Why, yes" is more like "Yes, of course" (The "why" is said in front to give the sentence emphasis.)
Why, no. I didn't know you were his cousin.
Wow. No. I didn't know you were his cousin.
======================================
I liked what Eliot said in his comment:
The second conversation is a bit difficult. Depending on where the emphasis is in each sentence, the meaning changes. Technically, the "why" shows some mild surprise, but in a humble way. It's rather old fashioned now to be humble in standard conversation. But in England we still use these forms of speech in the Houses of Parliament.
=======================================
Here's the link if you want to know more about it:
ttp://www.italki.com/answers/question/79081.htm