Whatever is happening on disk is under the control of the OS. It gets complex when there are 3rd party tools that want to "improve" this process, that's why it's best to avoid low level tools that Apple didn't write, sadly some people must use them for features that the OS cannot do any other way.
If you use the Finder's eject icon it should take action to ensure all read & write processes are complete. It should 'sync' any updates that have been held in memory or buffers for that disk.
Finder will warn you when there is something that cannot be stopped (like a Terminal window that has it's 'current working directory' inside that disk). The same is true of Time Machine, it will try to finish pending read/writes if you want to eject mid-process. Normally the extra delay is not too noticable, sometimes it can look like it has hung up.
I'm not sure that I fully agree with the idea that Time Machine will defragment data, I think it is a mis-translated concept that is simply easier to use to describe a nebulous idea… 'backup thinning'
Time Machine generates backups hourly, as a part of that process it also needs to delete older copies, for example at the end of the day, only the last copy is needed, at the end of the week & month the same situation is true. It also needs to account for backups that didn't complete.
This means it will periodically remove a lot of files & update links to the structure that describes the backup history (it looks like a bunch of folders, but they have metadata on them too).
I'm unclear on what point it does this purging, I have log entries that state…
com.apple.backupd[37271] No pre-backup thinning needed: 733.6 MB requested (including padding), 150.88 GB available
Which suggests it happens before the backup proceeds.
You can usually watch this in the 'All messages' section of /Applications/Utilities/Console (filter on "com.apple.backupd").
Hope that helps & doesn't compound the confusion 🙂