A reasonable theory to explain why people who move to higher ranked schools end up getting higher marks than if they stayed in a lower ranked school is because of the difference in environment.
If you were at a not-so-competitive school, then if you top the grade, you really don't have a good indication of your performance relative to the state. So for example, you wouldn't know if your abilities are at the mark range 90 or if they are at 99. You would usually need some external reference to judge your ability.
Also, with less competitive schools, there is less motivation/pressure to excel academically because the work ethic peer pressure just isn't there. If you're at the top you're usually more complacent because you don't think you need anymore effort to maintain the top, so relative to the whole state you might not be doing so well.
Also, if you're at the top and you are stuck on something, generally there aren't many people (particularly within your peers) within the school that can help you.
Usually those who come out on top with impressive marks in lower ranked schools have some sort of external reference to gauge their performance relative to the top end of the state and usually tend to work on their own pace.
A more competitive school (e.g. a selective school) on the other hand has a much more stimulating environment academically. There is definitely some peer pressure to do well which increases motivation/pressure to excel.
Since it's harder to top a competitive school, you get a better idea of how you fare against the top end of the state. So for example, you would know whether you're at the 90 range or the 99 range.
Even if you do manage to top it, you would have a lower chance of being complacent beacuse those just below you are often snapping at your feet lol
Also, if you happen to get stuck on something, you generally can refer to your 'smarter' peers to help you, whereas that wouldn't be so readily available in a much lower ranked school.
However, in the end it's generally up to the person as an individual. I know people who just can't stand competitive environments and others who thrive on them...