Earlier quoted context omitted.
I would imagine it is illegal to abandon a ship in port
Illegal for whom? Surely it's illegal for the owner, or perhaps the captain at most, but not the crew?
And the other is that the port authority wants the ship gone, and not vandalized or becoming a hazard to navigation. So they will require someone to stay on board to maintain it and keep it secure. Which can include getting Immigration to forbid the crew entrance to the country. While they are in port, it's much like being in the secure area of an airport - they're in a gray area and aren't actually in any country. So when that happens, it is illegal for them to leave it.