After Roberto Bautista Agut gave Spain the early advantage with a 7-5 6-1 victory over Dusan Lajovic in the opening singles match, Djokovic got the better of old rival Nadal 6-2 7-6 (7/4) to force a decider, in which he and Troicki beat Pablo Carreno Busta and Feliciano Lopez 6-3 6-4.
Djokovic and Troicki fell 3-1 down in the opening set of the doubles contest, but then reeled off seven consecutive games before Djokovic held serve to seal the win.
"I'll remember this experience for the rest of my life," Djokovic said on court. "This is definitely one of the nicest moments in my career.
"I've been very fortunate and blessed to have an amazing career over the last 15 years but playing for the team, playing for the country with some of my best mates for a long, long time, you can't match that."
Earlier, Djokovic had one the hotly-anticipated clash against Nadal to keep Serbia alive.
For all the hype given to the meeting, the outcome was perhaps no surprise as Djokovic prevailed against the world number one on a hard court for a ninth straight time.
Djokovic, ranked second in the world, established his dominance early on, breaking in the opening and seventh games to get on top and closing the set with four straight aces.
Nadal had break opportunities in the sixth game of the second set but could not find a way past Djokovic, who overpowered Nadal in the tie-break.
"Every time I play Rafa we get to play a lot of exciting points," Djokovic said on the ATP website. "We had some incredible exchanges.
"I started the match well with everything working perfectly for me."
After that defeat, Nadal and his team decided he would sit out the decider.
"I have been playing a lot of tennis the last couple of days," Nadal said. "My team-mates have been playing great yesterday.
"My energy is a little bit lower than usual, because I played long yesterday, very long before yesterday, very long in (Perth) the last day. So it's a team decision and we believe in our team."
Serbia needed victory from Djokovic to keep the tie alive after Roberto Bautista Agut beat Dusan Lajovic 7-5 6-1 in the opening singles contest to put Spain on top.
"Dusan is playing at a great level but I played a very good match from my side," said Bautista Agut, who broke the Serbian five times.
"He was aggressive and stepping inside the baseline to manage the game, but I believed I could change it and I played a great end of the first set and a great second set."