Hailed as “massively significant” by Gibraltar's leader, it was signed separately by Spanish Foreign Minister Josep Borrell and David Lidington, Prime Minister Theresa May's effective deputy.
It must now be approved by the Spanish cabinet and ratified by the parliaments of both countries.
The treaty was part of a deal sealed in November between London and the European Union (EU) on Brexit, as were four other bilateral deals on the overseas British territory that has long been claimed by Spain.
For Madrid, it is “essential” that Gibraltar's departure from the EU, planned for March 29 with Britain, takes place “in an orderly way and in keeping with Spanish interests in terms of fighting fraud and tax evasion.”
According to the Spanish foreign ministry, the text stipulates that individuals and other entities in Gibraltar must register their tax residency in Spain if that is where they earn most of their revenue, own most of their assets or if a majority of their owners or managers live there.
Spain welcomed the treaty, saying it set “clear rules to more easily resolve conflicts of tax residence” and allowed for “the reduction and elimination of tax fraud and (other) effects that harm the Spanish Treasury, deriving from the nature of Gibraltar's tax regime.”
Spain has long criticised Gibraltar's low-tax regime, while the tiny territory argues it is a crucial part of its thriving, services-based economy.
In a statement, Gibraltar's leader Fabian Picardo said the Spanish government had pledged “that the effective implementation of this treaty will lead to Gibraltar being removed from the Spanish blacklist of tax haven
jurisdictions in the future.”
He welcomed the treaty as “massively significant” as it also meant “Spain recognises, for the first time in history, the existence of registered Gibraltarians.”
Borrell meanwhile welcomed “the first international agreement signed by Spain and the United Kingdom over Gibraltar since the Treaty of Utrecht,” which dates back to 1713 and saw Spain cede the “Rock” to Britain.
Madrid has a long-standing sovereignty claim on Gibraltar, a small rocky outcrop on Spain's southern tip.
Both are closely interlinked as thousands of residents from neighbouring southern Spain, an area with high unemployment, travel to Gibraltar every day for work.