It’s an odd sort of interjection. I wonder how it started.
I recently came a cropper when I used it in an email conversation with a junior member of staff whose first language is not English.
He passed a nice comment on a report I’ve sent him and I said:
“Why, thank you.”
He replied, not unreasonably, as though I’d politely asked “Why?”