The Transport for Wales rail franchise is to be brought under Welsh Government control from next February.
Ministers have confirmed the takeover from KeolisAmey. Day-to-day services will be run by a publicly-owned company.
It follows significant falls in passenger numbers during the Covid-19 pandemic.
Economy Minister Ken Skates said the government had stepped in "to stabilise the network and keep it running".
"The last few months have been extremely challenging for public transport in Wales and across the UK. Covid has significantly impacted passenger revenues," he said.
A "new partnership" with the company will also be developed to help deliver rail improvements such as integrated ticketing.
Kevin Thomas, chief executive of KeolisAmey Wales, said: "In light of Covid-19, we recognise the need for Welsh Government to have a sustainable way forward for delivering its ambitious objectives for rail.
"We are pleased to have agreed and put in place robust principles as we work on the details of a new agreement."
It comes after the financial risk of the Wales and Borders rail franchise, which is branded Transport for Wales, was taken over by taxpayers under an agreement signed in May.
At the time the Welsh Government said it was putting up to £65m into rail services.
KeolisAmey was awarded the franchise in 2018, taking over from Arriva Trains Wales.
Amey Keolis Infrastructure Ltd will continue to be responsible for infrastructure management on the Core Valley Lines, where the south Wales Metro upgrade is taking place, the Welsh Government added.
Russell George, economy spokesman for the Welsh Conservatives, said: "Given the track record of the Welsh Labour-led Government, its decision to take control of our vital train industry has not filled me with any hope.
"Given how reliant the people of Wales are on using trains, we cannot allow the Welsh Labour-led Government to run it into the ground, like it has with Cardiff Airport."
Source: BBC