There are two main ways to get this star, both require the Metal Cap power-up blocks. There are two places in Jolly Rodger Bay with green Metal Cap blocks, I've circled them in red on this map:
(The map is from the bottom screen of the DS version, as you might be able to tell from the Wario icon, but the locations are the same)
The cap nearest to the start is on a platform before the ship. Immediately after grabbing the Metal Cap, you must run towards the star and long-jump into the the water. You'll be spending a large portion of the cap's length sinking down into the pit. Once at the bottom, run for the star. The timing on this one is a little tricky as in most cases the cap will run out at the star, or just before. If you don't quite make this first method, don't worry, you are close to the second cap.
The second cap is in an underwater cave at the bottom of the pit, marked by a ring of coins. Once you grab the hat, run back into the water tunnel and head towards the star. The timing on this Metal Cap running out is a little bit more forgiving than the first and should be enough to get you to the star. If not, try long jumping into the water at the tunnel, as Metal Mario can move much quicker above water than he can run in it.
The Metal Cap, if you haven't already, can be unlocked from Course 6; Hazy Maze Cave. Head to the the underground lake with Nessie, and ride her head to the platform with the double doors. Enter the double doors and jump into the metallic pool to enter the Metal Cap Switch Course. Jump onto the big green button at the end of this short course to unlock the Metal Cap blocks. (Also, grab the 8 red coins in the Metal Cap Switch Course for another star)