"My goodness!" Mine? Goodness?
Solution 1:
The Phrase Doctor writes that my goodness is a minced oath:
My goodness --> My God
My gosh --> My God
So the phrase was used instead of swearing, with goodness being a euphemism. The Oxford English Dictionary adds that goodness! has an established meaning in this vein, writing:
In various exclamatory phrases, in which the original reference was to the goodness of God (cf. sense 2a above), as goodness gracious!, goodness (only) knows! , †for goodness!, for goodness' sake!, in the name of goodness!, (I wish) to goodness! , surely to goodness!, thank goodness!, etc., or simply goodness!
The first written usage is by Shakespeare, who wrote:
1623 Shakespeare & J. Fletcher Henry VIII Prol. 23 Therefore, for Goodnesse sake, and as you are knowne The First and Happiest Hearers of the Towne, Be sad, as we would make ye.
There isn't a notation about the first written use of my goodness, but it definitely uses the sense of goodness which has been around since the 1600s.
Solution 2:
It's a socially acceptable way of saying Oh my god! without offence. It's not just a pun, but also a subtle counter to those who say Oh my god by switching the offensive part to something completely opposite: cursing God (bad) -> goodness