it depends on where the BB reaches maximum acceleration. If it's inside the barrel then the energy measured at the muzzle will be the maximum joules it can achieve. A heavier BB could still be accelerating as it leaves the muzzle and doesn't reach maximum velocity until xx distance (double that of a 0.2g BB). Another way to think of it is the BB has had the energy applied but it takes time (in our case distance) before the full effect takes place. Think of a rocket taking off, it doesn't go from zero to maximum right away, it slowly creeps forward and accelerates every meter it climbs until it's at full velocity. The rocket power is the same at 0 feet as it is at 1000 feet but the rocket is travelling at 2 different speeds at those 2 distances. (ignore the fact that the rocket is fighting gravity and we will be fine with this analogy).
I suspect that part of the reason for heavier BB's to travel further is that they are accelerating for longer so don't start to slow down as quickly as a lighter weight BB which reaches maximum acceleration quicker over a shorter distance, reducing the overall flight time compared to a heavier BB. Friction will affect the heavier BB more and gravity will also have some effect but it doesn't seem to be linear when comparing 0.2 and 0.4g BB's
One way to test this would be to line up a couple of chrono's and shoot some heavier weight BB's through them to see at what distance the BB peaks in joules.