I know there's already a 'best answer' for this, but this would've been a bit big for a comment, and is still an answer.
So, I tested this a lot, because it kind of weirded me out, and I think it has to do with holding a different button at the same time as pressing the A button.
That is, when you run into the Pokémon Center to heal, you're often still pressing the D-pad as you press the A button, right?
I tried stopping first and carefully making sure to push the A button and nothing else several times, and nothing happened then. So it most likely has to do with multiple button-pressing.
But, as to why this was programmed into the games... I think the others are right, Game Freak is just showing off ._.