In short, it was due to Kremlin's arrogance and deliberate ignorance that time and again leads it to underestimating Chechens at its own peril. Kremlin picked up a fight it neither needed nor were prepared to see through. As the joke goes, Kremlin mistook Chechens for Czechs and expected to roll over the country as they did in Czechoslovakia in 1968. That's not what happens in Chechnya, ask Mongols.
In 1990, while the USSR was on the brink of collapse, Chechens declared independent Chechen Republic of Ichkeria. The Russian Federation was formed later and it didn't immediately recognize Ichkeria, hoping to trade the recognition for loyalty, according to some. In 1991, Ichkeria held presidential elections and chose former Soviet Airforce general Djokhar Dudaev as the president. Dudaev quickly came at odds with Kremlin, crushing its hopes for a puppet government in Chechnya. It went downhill from there, with Kremlin trying to pressure Dudaev into submission by supporting his opponents, and Dudaev distancing himself and Chechnya from Russia. Kremlin banked on former Soviet elite of Chechnya to replace Dudaev in a military coup. They bought and armed anti-Dudaev opposition which together with vacationing Russian servicemen (much like in Ukraine later) attempted to capture Grozny in November 1994. The attempt was crushed by Chechen forces and captured Russian servicemen were shown on TV as a proof of Russia's meddling in Chechnya. Left with no other options to replace Dudaev, Kremlin went for all out military invasion in December 1994.
On the paper, Chechnya didn't have much military forces. Kremlin estimated, that Dudaev only had few thousand loyalists, no heavy armor or airforce to speak of, and they would just run away in the face of an overwhelming force of the Russian Army. Their idea was that marching all the forces in south Russia (ranging from 40K to 120K men by different sources) into Chechnya and quickly securing key cities (Grozny, Gudermes, Argun) would be enough to win the war. The Russian MoD famously boasted that he can take Chechnya with one regiment in two hours.
Meanwhile Chechens were preparing for the worst. Aslan Maskhadov, a decorated former Soviet colonel, was in charge in Chechen forces. The forces were indeed small in numbers and resources. There was no way to defend the plains of Nothern Chechnya against an enemy that had total air and fire superiority, hence, almost half of Chechnya was given away without a battle (barring few skirmishes organized by locals). The strategy hinged on giving a battle in Grozny, to bleed the opponent as much as possible, and then retreating to mountains where Russians would have hard time using their armor. Chechens were to hold to mountains as much as possible, while organizing a guerrilla movement in occupied territories. No one expected to defeat Russia outright, every Chechen had served in the Soviet Army and knew all well of its endless supplies of men and armor, the idea was to beat it to the point where it would realize it cannot defeat us.
And it played out better than Chechens expected. Russians indeed tried to blitzkrieg Grozny and failed: the city terrain somewhat negated Russia's superiority in armor, and Chechens organized as small mobile infantry units used their knowledge of the terrain to ambush, hug, and inflict asymmetric casualties on its opponent, especially its armor. This obviously came as a surprise to Russians, the army panicked and started targeting everyone indiscriminately. This was perhaps the biggest mistake of all. Initially the invasion was portrait as deposing Duduaev's "dictatorship", there indeed were a lot of Chechens not happy with Dudaev and leaning towards Russia, and as Kremlin predicted, some Chechen servicemen were salary cats and abandoned Dudaev when push came to shove. However, this brutal attack on civilians rallied the nation against Russia. Chechens from all over were traveling home to join the resistance. Most decorated Chechen commanders of the war were these civilians who picked up arms during the war and had no prior military experience. It took the Russian Army months to capture Grozny at an immense cost in men and armor, and what's worse, it failed to destroy the Chechen forces which retreated to mountains as was planned. It didn't get any better for Kremlin from there: the mountains were held far better than expected, a small village of Bamut, held by a hundred or so men, withstood three large scale Russian offensives and multiple attacks for more than a year, it was finally abandoned when the men were need to recapture Grozny in August 1996.
In the two years of the war, Kremlin committed every mistake and crime possible from letting its army loose on civilians, fueling their hatred against Russia, to failing to properly prop up their puppet regime and gain support even from those who were pro-Russia initially. The war lost what little support it had in Russia itself, despite the constant propaganda on TV. Kremlin did manage to assassinate the Chechen leader Djokhar Dudaev, and at the its forces controlled most of Chechnya. But at that point, the war for Chechens was no longer about Dudaev, politics, or territories, it was a matter of survival of the nation itself. Thus in spring of 1996 the Chechen HQ decided to recapture Grozny. They made few faint attacks on the city to let Russians believe that Chechens cannot mount any serious attack and were on their last breath. In a way it was true, it's said that when Aslan Maskhadov were signing the order for the final attack on Grozny, it only listed 820 men under his command. Yet when these thousand men entered Grozny in August 1996, they were supported by a few thousand locals and together they surround and cut off all Russian bases and checkpoints in the city effectively taking them hostage. There was no way for Kremlin to recapture Grozny without bombing their own troops surrounded in the city, hence, the peace talks began.
It all ended in September 1996 with signing of Khasavyurt accord, where Russia was to remove all its troops from Chechnya and start recognition process which was to end in 10 years, meeting most Chechen demands. Instead of capturing Chechnya in 2 hours with one regiment, as that MoD boasted, it took Russia countless regiments and two years to suffer a humiliating defeat at the hands of a much smaller enemy.