Decker wasn't demoted for no reason. Kirk demotes him because he believes he has more experience than Decker at commanding a starship, which he does.
I find it very doubtful that Kirk demoted Decker on his own authority, more likely it was part of the orders from Nogura. How it was handled was absurd, though. More realistically, Nogura or someone else senior to Kirk would have informed Decker before Kirk showed up, then Kirk and Decker could meet in private to discuss the transfer of command. Dramatic license, I know, but dumb.