When a reference has Persistent Location or Location Reference, it's also listed in these locations, creating bi-directional connection - locations have links to refs, refs have links to locations. The CK restores these links if they are broken. It's not necessary when a mod was made entirely in the CK, but if xEdit was involved, these links may be incomplete.

Nothing to worry about if the CK removes the ESM flag, it is weird this way.
You can resave plugins first, and then apply the script to avoid the extra step of restoring the flag.