The 1878 Agreement had all the elements of a valid contract: the Sultan agreed to lease what became Sabah to Messrs. Dent and Overbeck, in exchange for an annual sum.  There was no prohibition on the Sultan’s doing so, and no problem with his receiving compensation in return (though the amount, because it didn’t materially change, eventually failed to reflect the original bargain.)

These kinds of perpetual leases and similar agreements were common in 18th and 19th Century commerce and diplomacy.  The reason it’s still valid today is simple: the parties kept abiding by its terms.  Those terms were unchanged and straightforward – money for the use of territory and its resources.  True, the parties changed identity over time, but that was contemplated in the agreement itself (the contract was open-ended, and it referred to “successors-in-interest” to the original parties.)

A contract doesn’t lose its validity out of age; it just usually expires or falls into mutual disuse.  Neither happened here.