According to selected bits of information within this FAQ support page, the second iMac
has to be set up to use the Target Display Mode; if it actually is one that can use it...
• Thunderbolt ports and displays: Frequently asked questions (FAQ) - Apple Support
Some of those links within links do a 'circular logic thing' and go back to where you started.
And certain models of iMac or other Mac may not qualify for use as or with Target Display.
Main instructions suggest the Target Display Mode is interrupted by use of the same keyboard
shortcuts which enabled it. The main page previously linked could be used to help troubleshoot.
from: • Use your iMac as a display with Target Display Mode - Apple Support
Also, if the computers themselves haven't been re-started in awhile, they may benefit from the
cycling of a restart; in some instances, to reset the SMC may help. And/or the NVRAM. These
can affect screen resolution, and other aspects that may also relate to hardware performance.
• How to Reset NVRAM on your Mac - Apple Support
• Reset the System Management Controller (SMC) on your Mac - Apple Support
Are both iMacs running the same OS X? Not that this should matter; so long as both actually
are using Thunderbolt. An issue may exist if one uses MiniDisplay Ports, not Thunderbolt.
In some cases, the cable itself may be partially suspect, if issues relate to troubleshooting,
such items would be tested against another known-good cable in the process of checking.
Not sure what else to suggest at this point, and other obligations
will take me away from my Mac and the internet in general.
Hopefully another contributor can suggest other ideas on this.
Good luck!