Two microcontrollers, one common output
This is typical application for a digital multiplexer. In this case 8:4 multiplexer. For example the 74HC257 quad 2:1 MUX can do this.

Important thing is to keep its power on while either of the MCUs is on because input of inactive CMOS circuit is essentialy shorted to ground (via the protection diode to VCC).