There are no Scrum police and there is no, one right way to do Scrum. Scrum is a loose framework defined in less than 20 pages. Frameworks are a guideline, not a straight jacket. There are organizations, people, and thoughts that would have you think different. At the end of the day, remember, 20 pages is not a process, it's a guide, use your judgment.
That disclaimer aside, there are good starting practices most follow:
Role vs. Job is a huge distinction in Scrum. Someone should be filling the role, that isn't always their only job.
When coaching teams I generally go with this logic:
With a new team, a dedicated person, as Scrum master, can handle really only one team.
If you have one performing team and a newer team, a dedicated person, as Scrum Master, can handle two teams.
Once you get to three teams, then even a dedicated person can't fulfill the role of Scrum Master on all three teams easily. They have become a Scrum Coach.
Beyond four teams and you're moving into Agile Coach space as you are dealing with more and more concepts outside the outside the "strict" confines of Scrum.
If a team no longer has a dedicated Scrum Master role (because they are a higher performing team), the role of Scrum Master typically ends up being owned by the team. It's up to the team how to own it. They may rotate the role, divvy the role or any number of solutions. Because they are self-organized, they do this themselves and decide themselves.
And remember, always reach back to the Scrum Values and the Agile Manifesto to get a temperature check on how your decisions reflect the underlying principles and values.