Spain's Prime Minister Pedro Sanchez, whose country claims the rock should be part of its territory, withdrew a threat to boycott Sunday's European Council just hours before Britain's Theresa May was due in Brussels.
The British premier plans to meet EU leaders Jean-Claude Juncker and Donald Tusk, even though diplomats say the agreement is ready for EU leaders to approve.
Ahead of her arrival, diplomats had scrambled for an unexpectedly intense final round of discussion, after Spain insisted on keeping a veto over future changes to EU ties with Gibraltar.
Then Britain issued a statement saying it would continue bilateral talks with Spain after Brexit on 29 March -- and Sanchez relented.
"I have just announced to the King that Spain has reached an agreement on Gibraltar," he told a news conference.
"The European Council will therefore be held tomorrow. Europe and the United Kingdom have accepted Spain's demands. Spain has lifted the veto and will vote in favour of Brexit."
Gibraltar, a rocky outcrop home to a port and around 30,000 people, is a British territory claimed by Spain and a bone of contention as London negotiates a new relationship with Brussels beyond Brexit.
Even as Sanchez was speaking, the president of the European Council, Donald Tusk, was finally able to issue his letter inviting the leaders of EU member states to Sunday's summit.
"I will recommend that on Sunday we approve the outcome of the Brexit negotiations," Tusk said, saying the deal on the table reduces "the risks and losses resulting from the United Kingdom's withdrawal".
"And although no-one will have reasons to be happy on that day, there is one thing I would like to stress: at this critical time, the EU27 has passed the test of unity and solidarity," he added.
According to Tusk's invitation, the withdrawal agreement protects citizens' rights and the Northern Ireland peace process, while ensuring Britain will keep paying EU dues during a transition period.