Your cassette lockring should never come loose. It should be tightened to ~50Nm~50Nm ~40Nm which is a lot.
I think it’s more likely that your freehub body (together with the cassette) came off the rear hub. On some rear hubs (e.g. DT Swiss 240 and 350) you can just pull it off with some force. They just have a slight friction fit on the axle. Pulling the freehub body off exposes the pawls of the freewheel mechanism. Make sure you still have all the pawls and their springs and they are inserted correctly.