SYDNEY (AFP) An Australian navy diver was seriously injured as he fought off a shark in a rare attack in Sydney Harbour Wednesday, officials said.

The 31-year-old diver was taking part in a defence exercise near an upmarket residential area of the harbour when he was attacked, suffering severe injuries to his right hand and thigh.

Able Seaman Paul Degelder was rushed straight into surgery at Sydney's St Vincent's Hospital, where he was reported to be in a serious but stable condition.

The attack took place near the Garden Island Naval Base in Woolloomooloo Bay, which is lined with seafood restaurants and celebrity apartments.

The diver was taking part in a trial of new technologies to protect ports and ships from underwater attack but was on the surface when the shark struck, officials said.

"It all happened very quickly," the commander of the Australian fleet, Rear Admiral Nigel Coates, told the Australian Broadcasting Corporation (ABC).

"He fought the shark, punched it a few times. The shark disappeared. Our diver then swam to our nearby safety boat."