"We are very happy," said Kalle Isaksson at the Swedish Coast Guard (Kustbevakningen).
The whale was first spotted in the afternoon. By then it had managed to get itself stuck under a bridge between Fisketången and Valberget in Kungshamn.
The whale quickly managed to extricate itself from the bridge, but was then left stranded in the harbour. It was unclear to those in attendance whether the whale had got itself stuck on something underwater.
The commotion drew a significant crowd of people who lined the piers and nearby jetties.
"The whale appears to be aground at the moment. It's back is above the water surface and is splashing and spraying some water but seems to be have a tough time of it," said eyewitness Ellen Börjesson to the TT news agency at 8.30pm.
Local experts surmised that it was probably a humpback whale at of size of more three metres long. The coast guard said that three whales had been spotted at the location before.
The coast guard worked feverishly with other authorities including the emergency services, Marine and Water Authority and the Natural History Museum in Gothenburg to save the whale.
First, it was hoped that the whale would find its own way out the port, if it only it was left in peace and quiet. But this plan proved unsuccessful and instead they set about trying to scare the whale out using light and sound.
"You can hit metal objects together underwater or onto metal objects that protrude into the water," said Erland Lettevall, an investigator working with marine mammals at the Swedish Coast Guard.
The marine drama came to a close at around 9pm as the whale finally managed to find its way out of the harbour by its own volition.
"We followed it a couple of hundred meters," said Kalle Isaksson.
Whales occasionally stray close to shore, usually when they are looking for food, become frightened by a ship, or are injured and close to death.
The whale in Kunsghamn appeared to be suffering from injuries on its back which were probably caused by a propeller or the time it had spent trapped under the bridge.