I've never had the need to look at it that deeply, but I'll do my best.
Think in terms of there being two types of aviation regulations. In the GA world, think of them in terms of (1) rules which apply to the privileges and limitations of pilots and (2) "rules of the road."
There are ICAO member state agreements. One is the Chicago Convention on international aviation, which provides, among other things, that the "rules of the road" of the country you are in - airspace requirements, speed limits, for example, apply. On the other hand, "certificates of competency and iicenses issued or rendered valid by the contracting State in which the aircraft is registered, shall be recognized as valid by the other contracting States," so long as ICAO minimum standards are met.
Basically, that means, a US-certificated pilot flying a US-registered aircraft in foreign airspace has all of the privileges and limitations of his or her US pilot certificate. It also explains why a foreign country is free to not recognize US sport pilots or pilots operating under BasicMed - the standards for those don't meet ICAO minimum standards.