I'm not sure exactly how canon it is but according to the pirates wikiPirates Wiki page on the compassCompass:
Jack Sparrow's compass was an unusual navigational instrument most notably used by Captain Jack Sparrow, which he bartered from the voodoo mystic Tia Dalma and later inherited from his captain.
So apparently he bartered it from Tia Dalma, somehow lost it, gave it away, or alternative and then got it back in the flashback scene we see in Dead men tell no talesDead Men Tell no Tales.
The same page goes on to say:
However, the compass somehow found its way into the hands of the pirate captain of the Wicked Wench, under whom Jack was serving at the time.
So it appears to be unclear how the compass left Jack after he bartered it off of Tia Dalma till it comes back to him again.