Even if Scotty was second officer, his job as Chief Engineer might take precedence in some situations, and he might have the option to decline (or, more likely, Kirk has the option to pick someone else) if he is needed in engineering.
There probably is an official list of the command chain going down several links, but a Captain's prerogative has always been to allow anyone of his choosing to take acting Command for short stints.
This could be the primary function of the Goldshirt/Command Department, to show which officers are qualified to take command (...although many, like Spock and Scotty, choose not to wear the colour).
Maybe it's easier for Kirk to say "I'm beaming down to the planet. DeSalle, you're in charge," rather than contacting Scotty who might be in engineering or asleep or recovering from a massive hangover somewhere.
Chekov and Sulu are qualified to command (and are on a command track, as they eventually become an XO and captain, respectively), but might be redshirts otherwise. Like Detmer in Discovery or the random redshirt (i.e. Leslie) we see from time-to-time at navigation on the Enterprise who might not be qualified.