This means that something else must have taken 5 J of the total mechanical energy in your energy diagram. Your first equation to calculate the spring coefficient is correct:
$$mg = kx$$
But in this scenario, some external force has dampened the system, i.e. it has brought the mass to rest on the spring, which must be taken into account when considering the energy diagram.
This is the differential equation that describes motion on a spring when there are no external forces involved:
$$m \ddot{x} = mg - kx \quad \text{or} \quad \ddot{x} + \omega^2 x = g$$
where $\omega^2 = \frac{k}{m}$. What this means is that when you release the mass from its rest state, it will never stop oscillating. The solution to the above differential equation is:
$$x(t) = \frac{g}{\omega^2} ( 1 - \cos(\omega t)) \quad \text{and} \quad \dot{x}(t) = \frac{g}{\omega} \sin(\omega t)$$
where $x$ is displacement and $\dot{x}$ is velocity of the mass.
In your energy diagram from which you were calculating the spring coefficient $k$, you were calculating the mechanical energy for the displacement
$$x(t^\star) = \frac{mg}{k} = \frac{g}{\omega^2}$$
where $t^\star$ is the time at which displacement $x$ takes the value of $\frac{g}{\omega^2}$. Let's see what is the velocity at the time $t = t^\star$:
$$\text{from} \quad x(t^\star) = \frac{g}{\omega^2} = \frac{g}{\omega^2} (1 - \cos(\omega t^\star)) \quad \text{it follows} \quad \omega t^\star = \frac{\pi}{2} \quad \text{and} \quad \dot{x}(t^\star) = \frac{g}{\omega}$$
At that point there is also a kinetic energy you are forgetting:
$$K(t^\star) = \frac{1}{2} m v^2 = \frac{1}{2} m \frac{g^2}{\omega^2} = \frac{1}{2} \frac{(mg)^2}{k}$$
If you evaluate the above expression, you get the missing 5 J of the mechanical energy. In a scenario in which the mass is at rest on the spring, there must have been some external force which compensated kinetic energy so that the mass does not oscillate. The work from that force must also be taken into account in the energy diagram, and I showed here it equals exactly 5 J which was missing in your calculations.
The correct energy diagram of the spring-mass system without any external forces or dampening is as follows:
$$E = U_g + U_s + K = mgx + \frac{1}{2}kx^2 + \frac{1}{2}m\dot{x}^2$$
In the absence of external forces or dampening, the mechanical energy of the system always remains the same. In other words, the total mechanical energy $E$ is continuously distributed between gravitational and elastic potential energy, and kinetic energy. That is why the mass will oscillate forever. At some point the $U_p$ and $U_s$ will cancel out, but at that point all the mechanical energy $E$ is in $K$ which means the mass has the maximum velocity.
To conclude, your energy diagram for the total mechanical energy
$$mgx = \frac{1}{2} k x^2 \qquad \text{WRONG!}$$
is not correct because it does not include work from an external force that would bring the mass to rest on the spring. That is why you were getting the wrong value for $k$ when calculating it from the energy diagram. The correct calculation would be
$$\boxed{mgx + W_\text{ext} = \frac{1}{2} k x^2}$$
where $W_\text{ext} = -5 \text{ J}$.