In the military there is a saying that might shed some light on this: "Appointment supersedes rank."
As Memory Alpha notes (and I believe they're basing this on the U.S. Navy chain of command) a Commodore commands groups of ships.
So, the trick would be to determine whether the Commodore was officially in charge of a group of ships of which the Enterprise was a part of.
In the case of the episode 'The Deadly Years', Commodore Stocker is NOT in a position of command over the Enterprise. He is a guest--a passenger--enroute to his new command on Starbase 10.
Therefore, it would remain for the chain of command within the Enterprise to determine Kirk's fitness (McCoy's duty as Chief Medical Officer, but this could be requested by a senior officer, too) and then if he is determined unfit (which he was) the second-in-command becomes the acting (lacking) 'Captain'.
In the episode Commodore Stocker orders Spock to begin a competency hearing: this is a bit of a stretch (he'd likely not need to do so at all; although, his input and advice would likely be sought out given his role--but, the senior crew of the Enterprise would not likely be obligated to do so).
Also in the episode Stocker takes command: this would be highly unlikely as the Commodore is a passenger on a ship with an established chain-of-command that he and every one else would be aware of (Some of this is, of course, speculation because I don't believe such details of the chain-of-command were specifically set-out in the ST universe or canon...but, I could be wrong here).
Link to Memory Alpha: http://memory-alpha.wikia.com/wiki/Commodore