With the sounds of the description of the symptoms happening with this engine, it seems there could possibly be a number of things to consider looking into.
1) As mentioned the idle air control valve getting to hot sometimes damaging the wire harness / connector and in rarer cases melting IAC components or just becoming sticky and out of sequence.
2) ^ In-turn of getting too hot could also create internal ECM " IAC driver diode failure and needs replaced as this is becoming more common nowadays it seems...But bear in mind heat doesn't seem to be the only thing making the IAC driver fail too so anything is possible to damage the ECM...
https://whipplesuperchargers.com/ind...&product_id=73
3) The relays are starting to fail internally. Try 2 new ones and keep the old ones onboard.
Also note that the fuel pump relay handles the key on power to signal the ECM and power up the fuel pump system.....The fuel pump relay should have some pink wires going to it + possibly a green wire too.
http://boatinfo.no/lib/mercruiser/ma...er36.html#/120
The Main power relay should have mostly red wires running to it.
http://boatinfo.no/lib/mercruiser/ma...ser36.html#/92
4) As mentioned corrosion on wires or wire connections could cause this type of issue too. Loose wire or battery connections too so be sure to check/wiggle all power and ground wires/cables while the key is on waiting for the power to restore itself.
5) Keep a feel of the ECM temperature to make sure it's doesn't seem to be getting to hot too...Maybe try lightly tapping on the ECM to see if it seems to help at all when the time seems needed.
6) Make sure the 50 amp circuit breaker is not tripping/popping out.
What year is your boat?