The 35-foot-long whale was first spotted swimming back and forth in Ventura Harbor, southern California, on Saturday afternoon.
Video showed it turning back as it reached shallow water by the rocky shore.
Humpback whales often move through the nearby Santa Barbara Channel
During one length of the marina, the mammal nearly hit a propeller as it crashed into the back of a boat.
It is unclear if it was sick or injured, said harbour master John Higgins.
"I know that earlier today when it was navigating… under the lower tide that it was bumping into boats and there was some superficial cuts and bruises to it, but I don't know to what extent," he told KABC-TV.
Rescuers used a hydrophone to play recorded whale sounds underwater in an unsuccessful attempt to guide the whale back to the ocean.
Dr Rachel Cartwright, a biology lecturer at California State University, said the noise could have bounced around the harbour and made it more disorientated.
She said: "In some ways, quiet might be the better bet."
Dr Cartwright said it was migration season for humpback whales, which often move through the nearby Santa Barbara Channel.
Among the first to spot the whale were people who live on boats in the marina, some of whom said they watched as it swam under their homes.