StraightDope has this one dissected pretty thoroughly.
http://www.straightdope.com/columns/read/546/are-ships-captains-allowed-to-marry-people-at-sea
TL;DR: No, there's nothing special about ship's captain job that lets them officiate a marriage, at least in the US Law.
In other words, a ship's Captain can officiate a marriage only in the same circumstances that anyone else on the ship can - e.g. the couple would either have to be under jurisdiction of a state that recognizes common-law marriage, or the person doing the marrying (captain or not) has to be a clergy member or JD or other qualification recognized by the state.
Relevant legal docs cited by StraightDope are:
For the US Navy: Code of Federal Regulations, Title 32, Subtitle A, Chapter VI, Subchapter A, Part 700, Subpart G, Rule 716, also known as 32 CFR 700.716:
"The commanding officer shall not perform a marriage ceremony on board his ship or aircraft. He shall not permit a marriage ceremony to be performed on board when the ship or aircraft is outside the territory of the United States, except: (a) In accordance with local laws … and (b) In the presence of a diplomatic or consular official of the United States."
Similarly, the official logbook supplied to ships' captains by the British Mercantile Marine Office warns that shipboard marriages performed by the captain are not legal. If the ship is registered in New York state, the captain can be fined or imprisoned.
They also further discuss Fisher vs. Fisher. court decision and why it doesn't mean that this myth is true.
Please note that what may cause the confusion are 2 facts:
The captains are allowed - actually required - to be the registrar of any marriages (along with other important events on the ship) via the ship's log. So as long as someone legal (captain or not) performed the ceremony, the captain will register the fact in an official document.
Captains are likely to be licensed to perform the marriage (especially on cruise ships) aside from their captain status; as all it takes is a mail-in certificate from some religion (Universal Life seems to be popular). I wouldn't be surprised if cruise line companies encourage this to increase the value proposition of their ships.