"He has been diagnosed with a rupture of the anterior cruciate ligament, a rupture of the external collateral ligament and a rupture of the popliteal tendon in his right leg," Real Madrid said in a statement.
Madrid captain Carvajal hurt his knee in the final minutes of the reigning champions' 2-0 win over Villarreal on Saturday night which saw the team pull level with La Liga leaders Barca, who face Alaves on Sunday.
Carvajal went down in agony after attempting to clear a ball away under pressure from Villarreal winger Yeremy Pino.
The right-back, who won Euro 2024 with Spain this summer, confirmed he would miss a few months himself on social media network Instagram.
"A serious cruciate ligament injury has been confirmed, I'm going to have to undergo surgery and be out for a few months," wrote Carvajal.
"I can't wait to start my recovery and come back like a beast. Thank you very much to everyone for your messages, I feel very loved."
Carvajal was offered support on social media by his team-mates and other celebrity figures.
"Keep up the good work Dani! You will come back stronger," wrote Spanish tennis star Carlos Alcaraz.
Last season Real Madrid missed Thibaut Courtois and Eder Militao for much of the campaign with similar knee injuries.
Spain will be without Carvajal in the months ahead too, along with Manchester City midfielder Rodri Hernandez, also sidelined with a serious knee injury.
Madrid also confirmed winger Vinicius Junior, who scored in the win over Villarreal, had suffered a neck injury, although Spanish reports expect him to be fit after the international break.