Rockets have range safety systems, which you could also call self-destructs. They are typically pyrotechnic (explosive) devices arranged to do things like split open fuel tanks. The idea is that a rocket which goes off course can be issued a flight termination command to avoid it going somewhere it shouldn't or impacting more-or-less intact, causing more damage than if it was broken up and a reasonable portion of the propellant aerosolized before striking the ground.
Warships have been scuttled (intentionally sunk) to prevent them from being captured and used by opposing forces. It has been done or attempted on multiple occasions in both World Wars. The usual method is by flooding. Ships typically have sea chests - openings in the hull used to draw in or expel water for cooling, fresh water production, wastewater disposal, or other purposes. Either valves can be opened or specific elements of the associated plumbing damaged to cause water to fill enough areas of the ship to sink it.
In neither case is there a timer. For rockets, every second matters, so flight termination would occur as soon as the decision is made and the command issued. For ships, scuttling takes time - potentially hours. The whole idea of a self-destruct countdown seems to be a dramatic device inspired by time bombs, which have been a thing (fiction or real-world) for a very long time, and/or real-world space launch for which countdowns were (surprisingly) inspired by fiction.
Note that in "Balance of Terror", there is no countdown. The Romulan commander staggers to a control console, appears to throw a switch in an exaggerated motion, and the vessel explodes immediately.
The first depiction in Star Trek of a built-in self-destruct system with a countdown was, I believe, in the the original ("TOS") third season episode "Let That Be Your Last Battlefield" in which Kirk gives the order as he competes with Bele for control of the Enterprise. In the scene, it served to build dramatic tension when Kirk used it to set up a "game of chicken". There was a jerry-rigged self-destruct in the TOS second season episode "The Doomsday Machine" in which Scotty improvised an engine overload on a 30-second delay aboard the Constellation. A countdown never figured into any other self-destruct scenario in TOS. I can't speak to any Trek media outside of live-action TV and movies; within those constraints, it only re-appeared in ST3:The Search for Spock and in TNG, after which it seemed to have become cemented in Trek lore.
As a spaceship counterpart to naval scuttling, it makes sense for the destruct system to operate on a timer to provide an opportunity for the crew to escape. On the other hand, a self-destruct could just as easily be designed to operate by remote control instead of a timer. Nevertheless, a ship in space being deliberately blown up isn't necessarily a good idea since the debris could become a significant hazard to other space vehicles. That's one reason why real-world satellites are decommissioned either by de-orbiting so that they burn up in Earth's atmosphere or are moved into stable "disposal" orbits, and real-world range safety systems are only used during the ascent phase.