Some of the lyrics were popular phrases before the song was written. The line "To the shores of Tripoli" refers to the First Barbary War, and specifically the Battle of Derne in 1805. After Lieutenant Presley O'Bannon and his Marines hoisted the American flag over the Old World for the first time, the phrase was added to the battle colors of the Corps. "The Halls of Montezuma" refers to the Battle of Chapultepec, during the Mexican-American War, where a force of Marines stormed Chapultepec Castle.
While the lyrics are said to date from the 19th century, no pre-20th century text is known. The author of the lyrics is likewise unknown. Legend has it that it was penned by a Marine on duty in Mexico. The unknown author transposed the phrases in the motto on the Colors so that the first two lines of the Hymn would read: "From the Halls of Montezuma, to the Shores of Tripoli", favoring euphony over chronology.
The music is from the Gendarmes' Duet from an 1867 revision of the 1859 opera Geneviève de Brabant by Jacques Offenbach, which debuted in Paris in 1859. Correspondence between Colonel Albert S. McLemore and Walter F. Smith (the second leader of the Marine Band) traces the tune: "Major Richard Wallach, USMC, says that in 1878, when he was in Paris, France, the aria to which the Marines' Hymn is now sung was a very popular one."
The name of the opera and a part of the chorus was secured from Major Wallach and forwarded to Mr. Smith, who replied:
"Major Wallach is to be congratulated upon a wonderfully accurate musical memory, for the aria of the Marine Hymn is certainly to be found in the opera, 'Genevieve de Brabant'... The melody is not in the exact form of the Marine Hymn, but is undoubtedly the aria from which it was taken. I am informed, however, by one of the members of the band, who has a Spanish wife, that the aria was one familiar to her childhood and it may, therefore, be a Spanish folk song."
John Philip Sousa once wrote:
"The melody of the 'Halls of Montezuma' is taken from Offenbach's comic opera, 'Geneviève de Brabant' and is sung by two gendarmes."
Some websites claim that the Marine Corps secured a copyright on the song on 19 August 1891, but this is in error; the copyright was vested on 18 August 1919. In 1929, the Commandant of the Marine Corps authorized the three verses of the Marines' Hymn as the official version, but changed the third and fourth lines:
Admiration of the nation,
we're the finest ever seen;
And we glory in the title
Of United States Marines.
First to fight for right and freedom
And to keep our honor clean;
We are proud to claim the title
Of United States Marine.
This older version can be heard in the 1950 film Halls of Montezuma. On 21 November 1942, Commandant Thomas Holcomb approved a change in the words of the first verse's fourth line from "On the land as on the sea" to "In the air, on land, and sea" to reflect the addition of aviation to the Corp's arsenal.