How to lock screen of host machine with TDM on Sierra
This should not happen at all if your iMac locks itself after a period of time without TDM.
To lock your iMac's account after a period of time, follow these steps:

Choose Apple menu &gt; System Preferences, click Security &amp; Privacy, then click General.

Click the lock icon  to unlock it, then enter an administrator name and password.

Click Advanced.

Select “Log out after... minutes of inactivity.”

Set the amount of time before the user is automatically logged out.


(quoted from https://support.apple.com/kb/PH18670?locale=en_US)
.
It definitively is supported to log out a user while using Target Display Mode (TDM):

Any apps that are open on your iMac remain open and active while your iMac is in Target Display Mode. (...)
If you've set your iMac to automatically log out, apps are paused or closed when this happens but your iMac remains in Target Display Mode.

From: https://support.apple.com/en-us/HT204592#options
.
Also check these options in System Preferences:

Energy Saver &gt; Wake for network access (uncheck)
Security &amp; Privacy &gt; General &gt; Require password immediately after sleep or screen saver begins (check)
Desktop &amp; Screen Saver &gt; Screen Saver &gt; Start after: 1 Minute

Please let me know if this solves the issue.