Well your guess is as good as mine. I think the real world explanation is likely something along the lines of the ship builders in the FX department being tasked to build multiple ship designs for a particular episode, and those designs not selected get shelved, and then later reused to save costs. In universe explanation is more along the lines that each ship design has a specific role it fills. Since the feds are not on a monetary system, at least one as we know it, it doesn't cost more or less to do this.
As to Nebulas and Galaxies, they were sister designs. The obvious reason is the Nebula is the TNG analogue to the Miranda class, and it was a way to make things interesting for fans.
But you make a good point. Why so many classes, and why do so many seem to hang around, but others get retired? Does this mean some classes are so well designed they stay around for almost a century, while others are considered failures? Or does this mean some ships were pulled out of mothballs to meet the need of a growing Federation, which had a fleet devastated by the Borg and the Dominion war?
For example, if the Miranda class was derived from the Constitution class, and the constitution class was retired after TUC (Caveat: we have to assume this as we never saw Connies in TNG; all we know is the Enterprise A was retired, not necessarily all the Constitutions), then why were the Mirandas still around? Yet both Excelsiors and Mirandas outlasted the Constellation Class and Ambassador class.