It's sure supply and demand, but that's not much of an answer. The real question is why is the equilibrium so far from the one anywhere else.
IMO, there are two main reasons:
There's a big advantage to working in the same time zone and in the same culture: An American firm would much rather hire people in the US than in India, in the same way that you don't see Indian companies outsourcing their top work to the US. The skill differential is irrelevant, whether there is one or not. Being nearby is relevant.
The other difference is value of the work. We don't hire people to do work that is worth less to us than what they charge, but we sure can hire them for a lot less if it's possible. The value of your code is dependent on what it does for your company: A developer at Google, for instance, can build more value than someone programming the internal systems for a tiny chain of restaurants. The US has a lot more large employers that would be willing to pay a lot of money for programming, because their revenue per employee is insane. There's enough demand for good developers that a smaller employer will get a lot less profit from them. In comparison, look at Spain's market: There's a lot less pressure on the upper bound competing for the best developers, and most of the things that need automating produce a lot less value, so the equilibrium is further down, so the same developer can make 4x more in the US than in Spain.
It's a bit like professional sports: Viewership and spectacle produced per athlete lead to very different economics across sports, even though it's not as if a TE in the NFL, a power forward in the NBA or a strong defender in the NHL are that different, but for the average salaries of those three positions are very different.