It's an exclamation (or perhaps more formally, interjection), either in ODO's sense 2 or OED's IV.7.b.
1 expressing surprise or indignation: why, that’s absurd!
2 used to add emphasis to a response: ‘You think so?’ ‘Why, yes.’
7. Used interjectionally, before a sentence or clause.
a. As an expression of surprise (sometimes only momentary or slight; sometimes involving protest), either in reply to a remark or question, or on perceiving something unexpected.
1893 M. Pemberton Iron Pirate i, Mary looked up suddenly..and said,..‘Why, I believe I've been asleep!’
b. Emphasizing or calling more or less abrupt attention to the statement following (as in the apodosis of a sentence), in opposition to a possible or vaguely apprehended doubt or objection.
1869 G. J. Whyte-Melville Songs & Verses 93 So he made for the gate,..And the chain being round it, why—over he flew!