The default operation of closing the display is your mac sleeps. As long as you have power and an external display connected at that moment, and possibly also a keyboard and pointing device, it enters closed display mode instead. Working through startup, shutdown, and sleep is not supported, and Apple's guidance used to be, "it's your problem, open it back up and start over".
Apple has lost it love of closed-display mode, and has removed the separate article about it.
You are combining that with Target Display Mode, a feature that is no longer supported if you have a MacBook Pro after 2019 or are running more modern MacOS on your MacBook than Mojave.
Use your iMac as a display with target display mode - Apple Support
Executive summary: no additional support is likely to be forthcoming.