There's nothing to stop the beer from going into the line, other than pressure from incoming gas, but that only lasts until the pressure equalizes. When you put the gas coupler on the keg post, the poppet is depressed and it's now a two way street. Normally it's not an issue due to gravity, but when you start rocking the keg you can potentially splash beer up in there.
In the future, take care to always keep the gas post higher than the liquid level, and don't get too carried away when rocking the keg so you don't splash beer up there.
Personally I would suggest the set-and-forget method of carbonation, which makes this sort of thing impossible.