The design of macOS is to detect a display being connected, request its capabilities (read the EDID on HDMI for example) and then provision the display. Most displays don’t send viable signals out all of the ports to save on costs. In your case, this is what causes the last connected Mac to grab the pixels.
Getting a KVM ensures that you can cable all the computers one time and they will detect the correct external display whether they are first or last and then subsequently honor your choice of which signal gets priority.
Unless your specific display lets you cycle across inputs, you will need to add hardware or change your cabling or change your procedure to accomplish your goal.
For instance, I have a Mac and a PC that both have Thunderbolt 3 / USB 4 so I use a CalDigit element hub which lets me swap in and out the two computers to the external display without needing extra cables. It serves my needs better than a KVM since I don't mind swapping the cable and want my audio unit, headphones, keyboard and mouse to all switch with the display. It also allows fast and expansive external storage to be ready for either device.
Depending on the exact Mac, exact cable choice and exact display there might be one of these options that is more optimal, but there’s not a good general idea software solution for KVM needs in my experience. If you can't get the display to swap input, you'll need to change a cable whether it's a hub, the existing cables or getting a KVM that suits your needs.