Spain has given an assurance there will be no repeat of the incident in which officials on the border with Gibraltar searched a British diplomatic bag, the Foreign Office has said.
Europe minister David Lidington said the British Embassy in Madrid had lodged a formal protest with the Ministry of Foreign Affairs following earlier representations by officials "at a senior level".
"In our protests we requested an urgent explanation of this incident from the Spanish government and sought assurances that there will be no further interference with the UK's official correspondence," he said in a Commons written statement.
"We have now received that explanation from the Spanish and have been assured that we will see no repeat of these actions."
The incident, which occurred on Friday, following months of rising tensions between London and Madrid over the disputed British overseas territory.
Mr Lidington said two bags had been opened by Spanish officials - despite being clearly marked as containing official UK correspondence and communications - in breach of the Vienna Convention on Diplomatic Relations and the principle of state immunity.
"We take any infringement of these principles very seriously," he said. "The UK strictly adheres to these principles and we expect other states to do the same."
In response to an urgent Commons question, Mr Lidington said the Spanish blamed "an error at a junior operational level" for what happened.
According to the explanation given to the British Government, once the more senior official present realised what was happening, he put a stop to it.
"We have had an assurance that this action is not going to be repeated and we trust that Spain will live fully up to its own obligations under the Vienna Convention and international law," Mr Lidington said.
There was anger at the Spanish action among MPs on all sides. Former Liberal Democrat leader Sir Menzies Campbell said it was "rather an easy explanation" to blame a junior official and said ministers were entitled to press Madrid further.
Tory backbencher Bob Neill accused the Spanish of "stooping to the level of Franco's government" while his fellow Conservative Andrew Rosindell called for the Spanish ambassador to be expelled.
For Labour, shadow foreign office minister John Spellar said: "These provocative and unlawful acts are not acceptable to this Parliament and are not acceptable to the British people. They cannot be ignored."
Spain sought to play down the incident. Foreign minister Jose Garcia-Margallo said the packages that were opened were not even diplomatic bags.
"There is no incident. If it is not a diplomatic bag, there is no diplomatic incident. A bag was opened, which was not a diplomatic bag," he was reported as saying by the BBC.
A Foreign Office spokesman said the bags were "clearly marked as the property of HM Government" and, as such, were covered by the Vienna Convention.