The joke hinges on the phrase
in my pajamas
In the first line is ambiguous, it can be read as
One morning, in my pajamas
meaning you are in your pajamas, or
an elephant in my pajamas
meaning the elephant is in your pajamas
Of course an elephant would never wear your pajamas, so the listener will naturally assume you are wearing the pajamas. However, the following line
How he got into my pajamas I'll never know.
Confirms the fact that the elephant was wearing the pajamas, which in itself is a funny visualisation, but on top of that to shoot an elephant wearing pajamas has never been heard of before.
The joke is based on misdirection, where the listener thinks one thing, and the teller says another
A similar joke is
When I was born, they threw away the mold.
Well, some of it grew back...
Being a play on the word mold, meaning either a form or a furry growth on dead things.