I've come to know this phenomena when learning about the exceptions to the aufbau principle. But there is no explanation of why 4f and 5d have similar energies (as I've read in some websites).
The energy of a hydrogenic orbital with principal quantum number $n$ is proportional to $-(Z/n)^2$, so as $n$ increases the orbitals become more and more bunched together.
In a multi-electron atom the degeneracy of orbitals with different orbital angular momentum $l$ is lifted, i.e. s/p/d/f orbitals are no longer equal in energy. Since the gap between $n = 2$ and $n = 3$ is fairly large, one would not expect the 2p and 3s orbitals to be particularly similar in energy.
But the gap between $n = 4$ and $n = 5$ is smaller, so it is inevitable and not all that surprising that some of those constituent subshells have similar energies when the degeneracy is lifted. This is how it looks like for lithium (diagram from Modern Spectroscopy 4th ed., Hollas, p 214). The diagram will not be the same for the 4f/5d elements – in particular, I would expect the 4p/4d/4f orbitals to have a more significant difference in energy – but the idea is the same.