I have read that when the electron is not under the influence of a nucleus, i.e. at $n = \infty$ , its energy is zero which makes sense and as comes or presents (sorry I don't know the correct word) in an energy level it loses energy and the energy is negative.
As it loses even more energy it goes to lower energy levels and since the energy is already negative the magnitude of energy increases which ultimately means its energy is reduced in comparison to the higher level which also makes perfect sense.
I understand this by referring to the atom as the solar system, if the energy of a planet is higher than the orbit requires its orbit increases or sometimes it leaves the planetary system and vice versa if its energy is less than required its orbit shrinks or it falls in the star. So if an electron wants to go in the lower shell it will lose its energy.
Why does the electron lose energy in comparison to when it is at $n=\infty$ ? Is it related to the attraction force of the nucleus? I understand this phenomenon but I want to know the "why" behind the loss of energy of electrons.