Following a signing ceremony in the Kremlin, Medvedev said: "The documents we have signed envisage that our countries will jointly undertake the necessary measures for counteracting threats to peace... and opposing acts of aggression.
"We will show each other all necessary support, including military support. A repeat of the Georgian aggression... would lead to a catastrophe on a regional scale, so no one should be in doubt that we will not allow new military adventures."
The agreement also states that Russia will take measures to support the functioning of the regions' financial and banking systems since the Russian rouble is their main currency.
Russia drew international condemnation after it sent its troops into Georgia last month after Tbilisi tried to retake South Ossetia.
Moscow said it had a moral duty to act to defend them from what it called a genocide by Georgia's military.
Western states have angered Russia by backing Georgia, which is seeking to join Nato against Moscow's wishes, over the conflict.
Russia's foreign ministry accused Nato of Cold War thinking on Wednesday after the alliance held high-level talks in Tbilisi this week.
In a statement, the ministry said: "We cannot view steps to intensify relations between the alliance and Georgia any other way than as encouragement for new adventures."
Medvedev signed the treaties with Eduard Kokoity, the South Ossetian leader, and Sergei Bagapsh, the Abkhaz leader.
The two men were given all the trappings accorded to sovereign heads of state, with their regions' flags displayed in the Kremlin and an announcer introducing them to guests in their national languages.
Only Nicaragua has followed Moscow's lead and recognised the enclaves as independent, despite a diplomatic drive by Russia to persuade its allies to grant them recognition.
Giga Bokeria, Georgia's deputy foreign minister, said: "As we were saying before, this is an unconcealed annexation of these territories by Russia. The rest is just a masquerade. It's a violation of Georgia's sovereignty and territorial integrity."