A map-reading glitch sent the 20-strong invasion force onto the beach at La Linea, the town on the frontier with the British colony, to the surprise of Spanish locals.
The marines were greeted by two local policeman who watched in amazement as the heavily armed troops rushed ashore from two launches on Sunday morning.
The mayor of La Linea, Juan Carlos Juarez, said: “They landed on our coast to confront a supposed enemy with typical Commando tactics.
“But we managed to hold them on the beach.”
Dominique Searle, editor of the Rock’s daily newspaper, The Gibraltar Chronicle said: “What a boob.”
“We don’t know who was in command of the invasion force but the feeling is he should brush-up on his map reading.
“But to be fair he was only a couple of hundred yards from the right spot.”
Spain and Britain are Nato allies but are locked in a long-running dispute about the sovereignty of Gibraltar.
Spain magnanimously spared Britain’s blushes by accepting the landing had been a genuine mistake.
“We are not going to protest. From our point of view the matter is closed,” a Foreign Ministry spokesman said.
The British Ministry of Defence said it was a situation it would “rather not have taken place”.
“Two landing craft from HMS Ocean accidentally entered Spanish territorial waters and in bad weather one landing craft landed on the beach a few yards over the Spanish side of the border,” a spokesman explained.
“About 20 Royal Marines disembarked for about five minutes and then the error was recognised and they all withdrew.”
The gaffe came at an unfortunate time as talks between Foreign Secretary Jack Straw and his Spanish counterpart, Josep Pique, aimed at ending the centuries old dispute over the rock have stirred up animosity in both countries.
Gibraltarians and their political leaders are furious at suggestions that sovereignty of the peninsula could be shared.
A deal on its future is likely to be reached in the next few months.