Assume a spring fixed to a ceiling, with a weight attached to the bottom of the spring. If you push up on the weight and hold it against the bottom of the spring such that the spring is initially unstretched and uncompressed, the total energy of the mass/spring system will be $mgh$, where $h$ is measured from the point of maximum spring stretch (currently unknown).
When you let go of the weight, it will fall until all of the gravitational potential energy of the system is converted into spring potential energy. To determine how far the weight will fall, solve the equation $mgh=1/2kx^2$ for $x$, where $x$ is also equal to $h$.
Note that this is the lowest point that the weight can reach, and as soon as the weight gets to this point, the spring will pull the weight up, but the weight will not get quite as high as the release point due to air drag, and other dissipative forces (e.g., a small amount of friction in the spring). The weight will continue to oscillate up and down, with a slowly decreasing amplitude, until it comes to rest somewhat below the release point. At this point, you will find that $mg=kx$. This means that your analysis above involves two completely different situations: one situation describes the maximum stretch in the spring, and the other situation describes the new equilibrium position of the spring when it stops oscillating.