The following is based on a decompiled version of Minecraft 1.9 using MCP 9.24 beta.
The bug
When a chunk gets unloaded the entity in the chunk are added to the list net.minecraft.world.World.unloadedEntityList
. When all entities are updated this list is cleared and the entities are removed from the list of loaded entities.
The problem is that commands are executed after a chunk is unloaded, but before the entities are unloaded:
Call order (MCP 9.24 beta)
1. Removing unloaded chunks ("Minecraft Server" line 767 -> "World Server" line 200)
2. Commands ("Minecraft Server" line 767 -> "World Server" line 216)
3. Updating entities ("Minecraft Server" line 778)
This causes bugs like MC-96131 where a chunk is unloaded, a command (or possibly something else as well) loads the chunk, the entities are not removed and their data becomes reset.
Version 1.12.2 also affected. Seems a bit more sporadic, though. Tags seem to be retained (I'm assuming since it's stored differently), but scoreboard values are lost.