How can I place .dmg files away from the desktop?
Solution 1:
What you're seeing is the mounted disk image, you need to eject them.
These are no different than a USB disk or flash drive that you attach or a CD/DVD media that you load into a drive. It will show up on your Desktop so you have an easy way to access them. Once you're done installing them, you can simply right click and then select Eject.
For example, I double clicked a (random) DMG file (Western Digital macOS Apps) that was in a different directory (from my Desktop) and the mounted file shows up on the Desktop. If you double click, you will see the contents (usually install scripts or the App to be copied to the /Applications folder). When you're done, right click and select Eject. Note, many newer apps (VirtualBox I know does this) will, as part of it's script eject and move the DMG to Trash for you.
If using Terminal, you can see them by listing the contents of /Volumes
:
% ls /Volumes
You cannot delete the item; if you drag it to the Trash, the trash icon will turn into the Eject Symbol.
Solution 2:
If you want to eject the mounted DMG, please see Allan's excellent answer - or more simply do a secondary click on the DMG icon and select the Eject
option.
However, if you want to keep the DMG mounted, but don't want it to appear on your desktop, you can do that too! Go to Finder → Preferences → General, and under "Show these items on the desktop:" uncheck "External disks".
Note that this will also cause USB keys and similar to not appear on the desktop; you can access them from the Sidebar in Finder.