When, why, and how did the Captains of ships get the legal right to conduct marriages? Is it just because they may be considered the highest leagal authority while at sea?
Captains of ships generally do NOT have the legal right to conduct marriages:
However, a court case—Fisher vs. Fisher in the New York Court of Appeals in 1929—is often cited as supporting the validity of a marriage performed at sea by a ship's captain, although an 1898 case in California (Norman vs. Norman) ruled in the opposite direction. The judgement in the 1929 case did not suggest that the master had the legal authority to perform a marriage at sea but that subject to certain conditions, the exchange of vows between a couple could constitute a valid marriage if they considered it so—a form of common-law marriage. (However, there is in many countries no validity in law for a common-law marriage, although some states do recognize such an arrangement as long as both of the couple are free to marry, they both consent to the arrangement, they cohabit, and they allow their friends and neighbors to think they are married.)
However—note, not a historically sourcable statement—it's not difficult to see how the Captain, whose word is famously believed to be law, can also marry couples whilst at sea. What I find fascinating is that this idea is so well engrained despite pretty much no basis for it. Predecessors to American maritime culture, such as the British, have similar legality with respect to Captains marrying couples. I can't speak to all maritime cultures, but this seems to be a myth.
Google books source, Marriage Customs of the World: An Encyclopedia of Dating Customs and Wedding, by George Monger, pg. 597.