The UN is supposedly against the annexation of land by any party in any type of conflict, but had there been any exceptions to that? And what must be theoretically be done for the UN to make such an exception? A peace deal both countries agree on, perhaps? Thanks
The UN does indeed accept annexation of land if it is agreed on by both parties to a conflict. Or, well, more accurate is to say it doesn't oppose it.
For example, the Indian annexation of Goa was not heavily criticized by the UN's top officials, though they called for a diplomatic solution. It never faced serious opposition in the form of a UNSC resolution, either.
There have also been a variety of negotiations that resolved border disputes. The UN has no issue there.
The UN has been ambivalent at various points, or unable to reach consensus on, annexations undertaken by force. But the real question, I think you'd be interested in knowing, is how the norm against annexation of territory in war developed. That is a very interesting question, and one that developed over numerous decades before solidifying further in the 1970s.
One of the best places to look is at the work of the International Law Commission, established by the UN General Assembly in the 1940s, meant to codify international law and promote it. To join the ILC, you have to be an expert on international law elected by the UN to the position from a roster of nominees by member governments. The ILC got to work in the few years after the end of WWII, and began to try and codify the rules of what was shaping up to be a new world. In 1949, they put forward a draft code of international law that outlawed the use of force aggressively, and in effect codified the idea that conquest was wrong. However, they didn't come to a real conclusion on the question of defensive annexation of territory. They drew a distinction, in short, between invading a country and annexing territory, and being invaded, fighting back the invader, and annexing some of the invader's territory. On that, they were far less certain of how to determine legality.
In discussions on a draft code of law in 1950 that would have outlawed annexation, for example, numerous people raised objections, saying that annexations vary widely and there needed to be a clearer formula on what was and was not allowed. There were disagreements on whether annexations done without the use of force should be legal, too. And Mr. Sandstrom, a commission member, proposed that the crime be defined as:
"The annexation of territories by the threat or use of force for an aggressive purpose, or otherwise, in a manner incompatible with the right of a State to independence"
This was largely accepted, but the language changed again when the draft code was published in 1954. It simply named annexations "contrary to international law" as problematic, without labeling it based on aggression. The question was technically unsettled, but I think I've already made quite clear that what was considered "contrary to international law" was itself ambiguous, and aggression appeared to be the requirement for an annexation to be taking place "contrary to international law". Other ILC documents around this time reinforce that idea. In 1949 meetings, ILC members brought up that, based on post-war agreements concluded with Germany and other states, only the "right to conquest" should be condemned, i.e. acquisition made through violation of proposed Article 8, which condemned the use of war as an instrument of national policy and urged states not to use force against other states. The representative who brought that up even said that territory could be considered part of reparations, or repatriation of territory previously taken, in the German case, which shouldn't be restricted. That's why the Draft Declaration on Rights and Duties (DDRD) said in Article 11 that acquisitions in violation of Article 9, which outlawed aggression, should not be recognized by the world. It wasn't that all annexation should be unrecognized.
This was not binding law, of course. But we can use this to chart UN and world opinion, in a way, of the legality of territory changing hands. Acquisition by aggression was frowned upon almost uniformly by the UN from its inception, though it has been ambivalent sometimes based on circumstances. Land annexations have been allowed in some cases as part of peace treaties. The real dispute/change has been over defensive annexations.
By the 1960s, we can see that the issue was still rather unresolved. A 1967 Special Committee report to the General Assembly, for example, said that there was a need to reiterate that acquisition of territory through the unlawful use of force was outlawed, and that this reinforced the idea that aggression could not result in territorial acquisition.
Realistically, this principle was never put to the test. By the time most territorial acquisitions get discussed by the UN, they are either gained by the aggressor or are resolved on their own, or the UN eventually condemns and then accepts them later on. One of the few cases where a territory was acquired by the defender was the case of Israel, which gained control of Jerusalem, the West Bank, the Golan Heights, and the Sinai. Israel's acquisition of these territories was followed by its annexation of the Golan Heights, and of Jerusalem. By this point, however, the world made the decision not to recognize these acts, calling them inconsistent with international law. The annexation of Jerusalem in 1980 and the Golan Heights in 1981 came after the 1970s shift that solidified world opinion and UN opinion against any acquisition of territory in war. Whether or not that applies to Israel's case has been disputed by Israel, but the point I'm making here is that world opinion solidified against any territorial acquisition and annexation of territory without a peace treaty accepted by both sides, largely in a fair negotiation. Defensive acquisition was taken off the table, as arguments began to focus on the idea that annexation in self-defense was disproportionate, and one of the more important principles of international law is proportionality. If the aggressor cannot legally annex territory, it would be disproportionate to allow the opposite, some scholars argued. Others simply decided that the proper interpretation of the inadmissibility of territorial acquisition by force, such as the language used in the UN Security Council Resolution regarding Israel's 1967 war (Resolution 242), applied to defensive acquisition as well (though some also argued that said resolution was nonbinding, since it was adopted under Chapter VI powers of the United Nations Security Council). At any rate, the solidification occurred without any real tests of defensive acquisition.