King Felipe raised the controversial topic as he addressed MPs and peers at the Royal Gallery in the Houses of Parliament, outside which the flag of Gibraltar was on display.
Conservative MPs had threatened to walk out of the speech if the monarch laid claim to the territory during his speech, but his considered remarks did not see this happen.
King Felipe said in the traditional address to both Houses of Parliament that Britain and Spain have a long-shared history and have often “stood shoulder to shoulder” as friends, partners and allies in the best interests of both countries.
He continued: “It is just as true, however, that during our rich and fruitful history there have also been estrangements, rivalries and disputes, but the work and determination of our governments, authorities and citizens have relegated such events to the past.
“I am certain that this resolve to overcome our differences will be even greater in the case of Gibraltar and I am confident that through the necessary dialogue and effort, our two governments will be able to work towards arrangements that are acceptable to all involved.”
Fabian Picardo, the chief minister of Gibraltar, gave the comments a guarded response, telling Sky News: “What the King has said really harks back to a time when the governments in Madrid and London might make decisions over the heads of the people of Gibraltar.”
He said Gibraltar’s sovereignty was “not up for discussion or negotiation” and the territory “will never accept giving up one iota of our British sovereignty”.
Conservative MP Andrew Rosindell earlier urged Prime Minister Theresa May to remind the King that Gibraltar is British, despite long-standing Spanish claims to the territory.
Speaking after the King’s address, Mr Rosindell told Sky News it was “inappropriate” for him to raise Gilbraltar, but the language “did not justify storming out”.
He said: “It was inappropriate to raise it on a state visit, and while it wasn’t that provocative, he misses the point which is that when he talks about arrangements that would be acceptable to all sides, it is only the people of Gibraltar who should be able to make this decision and they have done.
“It wouldn’t have justified storming out, although that was what we were talking about if he had gone further.”
Flags of the overseas territories and dependencies are regularly raised outside Parliament for state occasions like the King’s visit, and Gibraltar’s flag is placed in the middle because they are in alphabetical order.
King Felipe also addressed Brexit during his speech, which was watched by the PM and Labour leader Jeremy Corbyn.
He said any Brexit deal must give the thousands of his compatriots in Britain – and UK expats in Spain – “sufficient assurance and certainty” over their futures.
He said those citizens have a “legitimate expectation of decent and stable living conditions”, amid criticism from Brussels of the offer made by Mrs May.
King Felipe said Brexit “saddens” Spain but that it “fully respects” the will of the British people.