I believe that the practice of addressing the CO of any naval vessel, regardless of rank, as "Captain" is more of a naval tradition than an actual commissioned rank. For example, In WW2 CO's of US Navy submarines were Lieutenant Commanders by rank, but they were called "Captain" by their crews. John F. Kennedy was a Lieutenant when in command of PT-109 but he was referred to as the "captain" of the ship.
Indeed, however in Trek we see the Captains of everything down to piddly crews of 12 on the Lantree with four pips, indicating what the 19th Century RN would call a "post Captain". You could argue a prestige job, like for example Esteban in ST3 exploring political hot-potato Genesis, would require four pips even on a piddling science ship, but he still seems over-ranked for such a menial command.
Rank isn't the only thing that determines who is in charge of any given situation, it's also the position one is in. A Second Officer can't give the Chief Engineer an order, unless said Second Officer is acting as temporary Captain.
Quite, I assume you are referring to today's military here today as well. When HMS Conqueror sank the Belgrano, her Ex-O was a Lt. Cmdr and her engineering officer had a full three stripes, but the Ex-O through position was higher up the chain of command.