This is probably a duplication glitch in the game. The ender dragon is not allowed to teleport through the end gateways, but still attempts it.
Basically the dragon flies through the end gateway, causing it to teleport to the outer Island. Then the game realises that the dragon is not allowed to teleport the dragon and returns it to the main Island. It's the last bit where it goes wrong. The dragon gets duplicated and is both on the main Island and on the outer islands.
The dragon has a state in which it travels to the exit portal on the main Island. That's why the dragon travels of into the distance.
I can't make much more of it. Just be carefull when you enter any gateways.