Spain has reached a deal with the UK to maintain free movement to and from Gibraltar once the UK formally leaves the EU on Friday.
To avoid a hard border, Gibraltar will join the EU's Schengen zone and follow other EU rules, while remaining a British Overseas Territory.
The deal was announced by Spanish Foreign Minister Arancha González Laya, just hours before the UK exits the EU.
The Rock voted Remain in 2016 and about 15,000 Spanish workers go there daily.
"With this [agreement], the fence is removed, Schengen is applied to Gibraltar... it allows for the lifting of controls between Gibraltar and Spain," said Ms González Laya.
The Gibraltar deal will mean the EU sending Frontex border guards to facilitate free movement to and from Gibraltar. Their role is planned to last four years.
Gibraltarians are British citizens. They elect their own representatives to the territory's parliament, while the British monarch appoints a governor.
The territory - home to a British military garrison and naval base - is self-governing in all areas except defence and foreign policy.
Ms González Laya did not say whether Spanish border guards would eventually be posted at Gibraltar's airport and/or seaport which, under the deal, will be de facto part of the EU's external border.
The Gibraltar deal would also mean the territory complying with EU fair competition rules in areas such as financial policy, the environment and the labour market, Ms González Laya said.
Twenty-two EU states are in the passport-free Schengen zone, as are Norway, Switzerland, Iceland and Liechtenstein, but the UK has never been in it.
Once Gibraltar joins it, EU citizens arriving from Spain or another Schengen country will avoid passport checks, while arrivals from the UK will have to go through passport control, as is already the case.
UK Foreign Secretary Dominic Raab called Thursday's deal a "political framework" to form the basis of a separate treaty with the EU regarding Gibraltar.
The deal does not address the thorny issue of sovereignty. Spain has long disputed British sovereignty over the Rock which was ceded to Britain in 1713 and which is now home to about 34,000 people. The Remain vote there was an overwhelming 96% in the 2016 EU referendum.
The plan is to have a six-month transition period and then formalise the new arrangements with a treaty.
Under the current tight Covid rules, there are restrictions on UK citizens arriving via Gibraltar's airport, the UK Foreign Office says.
Dominic Raab said "all sides are committed to mitigating the effects of the end of the [Brexit] Transition Period on Gibraltar, and in particular ensure border fluidity, which is clearly in the best interests of the people living on both sides.
"We remain steadfast in our support for Gibraltar, and its sovereignty is safeguarded."