Target Display Mode troubles, can't disconnect anymore
I have the same issue and here’s why it’s a software bug.

On my 2013 21.5" iMac (base model with the only upgrade being a factory installed 256GB SSD). I've never had any work done on it.

I attached an external drive running 10.11 El Capitan and booted from it. Activated target display mode, then deactivated it. NO ISSUE, IT WORKED! So I thought maybe there's something corrupted in my install and I have to erase and start from scratch. Before I did that, I clean installed Mac OS High Sierra on my external drive. ISSUE REOCCURED on a fresh install! Then I installed the Mojave Beta to see if Apple fixed it. NOPE it still cannot deactivated Target Display mode without screwing up the internal monitor!

I've discovered though that plugging in an external DisplayPort display to the iMac "re-wakes" the internal display as it activates the external one. It's only slightly better than having to restart, as you still need to reach behind and plug in and out cables. Frustrating. I have an open ticket with Apple Support.

Basically, the problem isn't our computers or our installs. It's Mac OS High Sierra and Mojave (and maybe regular Sierra, I can't tell). El Capitan doesn't have this problem.