Textbooks won't bother to dig into details. Most of them are satisfied with saying that such thing exists. They're right I guess. I'll do a chemguide approach to the answer since I find it most understandable.
We draw this diagram, and then assume that it works for all atoms. In other words, we assume that the energies of the various levels are always going to be those we draw in this diagram.
If you stop and think about it, that has got to be wrong. As you move from element to element across the Periodic Table, you are adding extra protons to the nucleus, and extra electrons around the nucleus.
The various attractions and repulsions in the atoms are bound to change as you do this - and it is those attractions and repulsions which govern the energies of the various orbitals.
Going through the Aufbau principle, when you pass Ca (Z = 20) you will face Sc, the first element in which a d orbital is going to be filled. But, you must make a really important change in your train of thoughts.
As chemguide explained, with change in the number of protons and electrons due to the change of Z, the way their orbitals' energies are defined changes. Till Ca, the behavior insisted that 4s has a lower energy state than 3d and thus, fills sooner. But, starting from Sc, the pattern changes: In Scandium, 3d has a lower energy than 4s. The behavior changed.
This change in behavior results in the current confusion of yours. This much answers the problem you have, and you can visit the page at chemguide to learn more. If you want more info, I can freely add. Hope I've helped!