I sometimes have heard somebody replying with Why, thank you. instead of Thank you.
What is the meaning of the first phrase? What is the difference between the two phrases?
It is definitely expressing mild surprise. Imagine you're giving a dinner party. You turn to the person sitting at the table next to you and ask them to pass the salt. They do. You say "Thank you." You wouldn't say "Why, thank you.", because there's no surprise, you were fully expecting them to pass it to you. But if someone says "I must compliment you, this stew is delicious!", you might well say "Why, thank you!", because you weren't expecting the compliment, it was a bit of a surprise. You could still say just "Thank you." in this case, but you wouldn't really ever say "Why, thank you." in the first case.
Why is used here as an interjection. According to Merriam-Webster:
—used to express mild surprise, hesitation, approval, disapproval, or impatience <why, here's what I was looking for>
In my experience, the extra why in Why, thank you is used mainly to avoid appearing too abrupt in one's thankfulness.
The OED has four definitions of why under the sub-heading ‘Used interjectionally, before a sentence or clause’. They are:
as an expession of surprise;
emphasizing or calling more or less abrupt attention to the statement following . . . , in opposition to a possible or vaguely apprehended doubt or objection;
as an emphasized call or summons, expressing some degree of impatience;
anexpression of content, acquiescence, or relief.
Why derives from Old English hwæt, the first word of ‘Beowulf’, where it is used to attract the attention of the listeners, so it is perhaps not surprising to find it used in contexts such as these.