The first real hint of this was Voyager, which makes sense, because it took them 23 years to get back home, quarter of a century. In the revised timeline, only 7 years, but still makes sense. How many starships fly across the galaxy in under a decade? It was a PR thing. We also know the NX-01 ended up as a museum ship after a decade in space. This was also likely a PR with the formation of the Federation and being Starfleet's first explorer and a prototype at that.
In the 1966-2005 run, these are the only examples I can think of where a Starfleet starship became a museum piece. This wasn't really a thing in the reboot trilogy. In the streaming shows, (not counting Voyager in Lower Decks), this seems to mostly be just a "Picard show" thing. I don't like the idea that starships get retired after some 10-20 years. Cars last longer than that. WTF? The whole "mileage" thing reduces a starship to a car. Are these things supposed to go toe-to-toe with the Borg, the Dominion, survive crash landings, and shit? Come on. If the "story reason" is PR, trashed but made to look nice, or just horribly outdated (no one's expecting the Ent-A to fly again, LMAO), no probs. The Defiant-A is only 26 years old by PIC S3, why is it not still flying around kicking ass? Kirk's Enterprise was 20 years old when he took command and still had the new paint smell.
Getting back on the topic.
Even in the real modern day we have museum ships of all sizes. Sailing ships, steam ships, warships, etc.
In a world where profit is no longer the driving force, more ships can be preserved for younger generations to visit.
There has been talk of a fleet museum for some time, I cannot remember the episodes, but I'm sure it's been mentioned more than once. And I recall strongly it being referred to directly in Star Trek First Contact by Picard when he touches the Phoenix.
I too do not subscribe to this nonsense that ships are retired after a paltry 20 years service. This is revisionist crap. All through TNG, DS9 and a bit of VGR, we same designs that were easily 100 years old still in service. Others 40 years old still plying their trade.
The Galaxy Class for example is written as a design that is supposed to be in service for 100 years with modular technology allowing it to be upgraded periodically. It should be more than capable of being in service during PIC. But we see not a single one (until the big D appears).
Now, the real life reason is they did not use the Galaxy Class as they did not want people thinking it was the Enterprise D. (While most of us are clever enough to know it wouldn't be, there are some that would genuinely think it was. This is why we never saw an obviously unmodified Galaxy in DS9 until after ST Generations, after which they featured somewhat regularly).
There may be practical reasons for the USS Syracuse, the donor of the stardrive for the resurrected Enterprise-D, to have been taken out of service. Likely some damage that was deemed too extensive to be corrected for it to return to regular service. (As what happened to the Enterprise-F).
So, I support the idea of a fleet museum and than occasionally a fully capable ship might get retired and displayed for all sorts of reasons. But I reject this notion that a 20 year old ship is too old and MUST be retired.