In 1999, Portugal fully returned Macau to China. However, did they get something for giving Macau back to China, or was it just a sign of good faith and decolonization in general?
By giving up Macau quietly, Portugal avoided an embarrassment similar to the one they experienced when India took Goa.
More seriously, this was done as part of a treaty at a point in time where Portugal was carrying out a policy of de-colonization. Portugal basically offered to return Macao to China.
Portugal offered to withdraw from Macau in late 1974, but China declined the offer in favour of a later time because it sought to preserve international and local confidence in Hong Kong, which was still under British rule, and also because it prioritised the reunification of Taiwan.
In 1976, Portugal unilaterally changed the legal designation of Macau from a "colony" to "territory under Portuguese administration"
Both from here. So, it looks like Portugal wanted to leave Macau at any cost. And China wanted Macau to be the last extant European territory in continental Asia, instead of Hong Kong.