The 19-year-old will return to the Serb club on loan and join up with the Premier League side on 1 July.
"I'm very excited and so proud to be here," said Grujic, who won the Under-20 World Cup with Serbia in 2015.
The Reds outbid Anderlecht and Stuttgart and saw off interest from Russian sides CSKA Moscow and Zenit St Petersburg.
Grujic, who has also represented his country at under-21 level, has scored five goals in 22 games for Serbian Super Liga leaders Red Star this season.
He is Jurgen Klopp's first signing as Liverpool manager since his appointment in October.
"He's a good boy, a young boy but plays an important role at the moment at Red Star, who are the best team in Serbia," said the German.
"He's a tall boy, but quick and good at technical things. He can play passes and can dribble."
Find all the latest football transfers on our dedicated page.
Liverpool have signed Red Star Belgrade midfielder Marko Grujic for £5.1m on a four-year contract.