I can't exactly tell you why the service is not as poor as the UK, but there are some major differences. Deregulation of the rails has happened gradually, it started in the late 80s when the infrastructure division of Statens Järnvägar was split in to a separate agency, Banverket. And in the early 90's SJ lost parts of it's monopoly. The rail system has since then been more and more deregulated and in 2002 SJ was converted from a government agency to a company. The state still owns all the shares, but SJ still exists and runs most of the long distance trains but does not have a monopoly anymore. Unlike in the UK where BR was split up. But I have the impression that the companies in the UK are still very much under political control as there are franchise contracts that regulate what trains they have to run and so on. SJ basically acts as an open access operator that runs trains that are profitable. National routes that are deemed important but not profitable are tendered as PSO routes, currently that is a couple of night trains and some interregional trains. Deregulation of long distance buses also means that SJ has some competition even where there no other train operators
Local transport is operated by the regions (län). Every region is required to have a regional transport authority that work pretty similar to TfL. I.e. they set the fares, schedules and routes. But hire other companies to run the actual trains, buses and other forms of transportation. Routes that are not very profitable. Some are, but it's hard for private companies to compete since the systems are usually pretty well run. And subsidised. The reasoning behind that it that it can be cheaper in the long run to pay for a bus route than to not do it. But the market is not regulated, anyone can buy a bus and start a competing company. But the main difference here is that I think local councils are not allowed to run public transportation in the UK apart from London and Manchester? Rail deregulation also lead to an explosion of local and regional trains all over the country in the 90s. Before that SJ had a monopoly and could charge the local transport authorities whatever they wanted to runs trains. That lead to the situation where local trains more or less only existed around Stockholm, Gothenburg and Malmö. While in other parts of the countries local buses ran parallell to underused rail lines that only saw a few intercity trains and some freight trains per day. But due to deregulation it became a lot cheaper for the regional transport authorities to operate trains.