Gibraltar will remain part of Europe's Schengen area of free movement under a last-minute Brexit deal struck between Britain and Spain.
Fabian Picardo, the territory's chief minister, said the eleventh-hour "agreement in principle" would avert the "worst effects of hard Brexit".
Britain's foreign secretary Dominic Raab said it would "ensure border fluidity, which is clearly in the best interests of the people living on both sides".
Prime Minister Boris Johnson welcomed the agreement "wholeheartedly".
Spain's foreign minister Arancha Gonzalez Laya said it would mean people living in the territory "can breathe a sigh of relief".
She said that without the agreement, tens of thousands of Spaniards and Gibraltarians who cross the border every day would have been forced to go through checks and passport stamping.
Technical details such as how Gibraltar's port and airport will be policed will be published in the new year, she added.
Mr Picardo said: "This has not been easy and we have gone to the wire.
"We are so close to the wire in fact that I think all of us involved in the negotiations felt the wire cutting into our flesh."
London and Madrid had been negotiating how to police the land border between Spain and Gibraltar which was excluded from the last-minute exit deal reached between Britain and the EU last week.
Mr Picardo warned that there were still tough negotiations ahead and that "we may end up with a deal on the (free) movement of people but not goods".
A major hurdle in the negotiations has been whether EU representatives could be stationed in the British territory. Spain will be responsible for implementing Schengen rules.
Mr Picardo said this would be managed by the introduction of a Frontex operation - referring to Europe's border agency - to control "entry and exit points from the Schengen area at the Gibraltar entry points" for an initial period of four years.
The chief minister added: "We could not allow a backward step in the mobility of people to become the new normal.
"As the world becomes more united, we could not see ourselves more cut off from the physical continent of Europe."
Mr Raab said that following "intensive discussions" the two sides had reached agreement on a political framework as the basis of a separate treaty to be negotiated between the UK and the EU over the territory.
"In the meantime, all sides are committed to mitigating the effects of the end of the transition period on Gibraltar, and in particular ensure border fluidity, which is clearly in the best interests of the people living on both sides," he said.
"We remain steadfast in our support for Gibraltar, and its sovereignty is safeguarded."
Ms Gonzalez Laya said she expected the new treaty to be signed within six months.
Home to around 34,000 people, Gibraltar relies on access to the EU market for its economy. More than 15,000 people live in Spain and work in Gibraltar.
In the Brexit referendum of 2016, 96% of voters in Gibraltar supported remaining in the EU.
Without the deal announced with hours to spare on Thursday, they faced entering the new year with tight controls on what for decades has been an open border with Spain.
Gibraltar has been a British territory since 1713, with its citizens voting overwhelmingly, with 99% in favour, to remain under UK sovereignty at a 2002 referendum.
However, Spain has long maintained its own claim to Gibraltar, which sits at the southern tip of the Iberian peninsula, and has previously used the Brexit process to press the issue.