ScotRail will be run by a new company owned by the Scottish Government from March next year.
An arms-length firm will be set up as an “operator of last resort” to take over when the current Abellio franchise ends.
The Dutch firm has been repeatedly criticised for its running of Scotland’s passenger trains since it won the contract in 2015.
Meanwhile, the UK Rail Review said the current franchising system could not continue in its current state.
All ScotRail staff will transfer over to the new Scottish Government-owned firm, the transport secretary Michael Matheson said.
Matheson said: “ScotRail staff will transfer to the new Scottish Government owned entity, with their terms and conditions protected.
“This approach will provide a stable platform for ScotRail services and certainty for passengers and staff.”
Union leaders said forming a public sector body to run the trains would “deliver significant benefits”.
RMT union general secretary Mick Cash said: “Public ownership of ScotRail will deliver significant benefits for Scotland’s rail workers, passengers and taxpayers and help enable the creation of an affordable, accessible and sustainable rail network that contributes to Scotland’s decarbonisation targets.
“In light of today’s announcement, RMT will be seeking urgent discussions with the Scottish Government to ensure that all ScotRail workers’ jobs, pay and conditions are protected throughout this process and once they transfer to the new operator.”
Opposition political parties also responded to the nationalisation.
Scottish Labour transport spokesperson Alex Rowley said: “It is welcome news that the SNP have finally seen the light and decided that nationalisation of our railways is the best way forward.
“When SNP ministers awarded the contract to Abellio they told us we would have world-leading services but for far too many that was not the experience.”
Graham Simpson, the Scottish Conservatives shadow transport secretary, said: “The SNP government must be entirely honest about how much this will cost taxpayers and clear about what demonstrable benefits it is expected to bring to passengers.”
He added: “This is the same government that can’t even build a couple of ferries, which hardly instils confidence in them having the competence to run a rail service.”