The story of encounter of Yaksha and Pandava is mentioned in Mahabharata SECTION CCCX and SECTION CCCX.
During the vanavas, Pandavas were wandering in forests and once they felt thirsty. Then Yudhishthira has asked to Nakula to look for the water. He found a lake and when he is going to drink water, a hidden voice has warned him that he could not drink water before answering his questions. But Nakula has ignored that voice and drank the water and felt dead on ground. Same thing happened with Sahadeva , Arjuna and Bhimasen.
Then king Yudhishthira addressed Nakula saying, 'Do thou, O son of Madri, climb this tree and look around the ten points of the horizon. Do thou see whether there is water near us or such trees as grow on watery grounds! O child, these thy brothers are all fatigued and thirsty.' Thereupon saying, 'So be it,' Nakula speedily climbed up a tree, and having looked around, said unto his eldest brother, 'O king, I see many a tree that groweth by the water-side, and I hear also the cries of cranes. Therefore, without doubt, water must be somewhere here.' Hearing these words, Kunti's son Yudhishthira, firm in truth, said, 'O amiable one, go thou and fetch water in these quivers!' Saying, 'So be it,' at the command of his eldest brother Nakula quickly proceeded towards the place where there was water and soon came upon it. And beholding a crystal lake inhabited by cranes he desired to drink of it, when he heard these words from the sky, 'O child, do not commit this rash act! This lake hath already been in my possession. Do thou, O son of Madri, first answer my questions and then drink of this water and take away (as much as thou requirest). Nakula, however, who was exceedingly thirsty, disregarding these words, drank of the cool water, and having drunk of it, dropped down dead. And, O represser of foes, seeing Nakula's delay, Yudhishthira the son of Kunti said unto Sahadeva, the heroic brother of Nakula, 'O Sahadeva, it is long since our brother, he who was born immediately before thee, hath gone from hence! Do thou, therefore, go and bring back thy uterine brother, together with water.' At this, Sahadeva, saying, 'So be it,' set out in that direction; and coming to the spot, beheld his brother lying dead on the ground. And afflicted at the death of his brother, and suffering severely from thirst, he advanced towards the water, when these words were heard by him, 'O child, do not commit this rash act! This lake hath already been in my possession. First answer my question, and then drink of the water and take away as much as thou mayst require.' Sahadeva, however, who was extremely thirsty, disregarding these words, drank of the water, and having drunk of it, dropped down dead. Then Yudhishthira, the son of Kunti, said unto Vijaya, 'It is long since, O Vibhatsu, that thy two brothers have gone, O represser of foes! Blessed be thou! Do thou bring them back, together with water. Thou art, O child, the refuge of us all when plunged in distress!' Thus addressed, the intelligent Gudakesa, taking his bow and arrows and also his naked sword, set out tor that lake of waters. And reaching that spot, he whose car was drawn by white steeds beheld those tigers among men, his two younger brothers who had come to fetch water, lying dead there. And seeing them as if asleep, that lion among men, exceedingly aggrieved, raised his bow and began to look around that wood. But he found none in that mighty forest. And, being fatigued, he who was capable of drawing the bow by his left hand as well, rushed in the direction of the water. And as he was rushing (towards the water), he heard these words from the sky, 'Why dost thou approach this water? Thou shalt not be able to drink of it by force. If thou, O Kaunteya, can answer the question I will put to thee, then only shalt thou drink of the water and take away as much as thou requirest, O Bharata!' Thus forbidden, the son of Pritha said, 'Do thou forbid me by appearing before me! And when thou shalt be sorely pierced with my arrows, thou wilt not then again speak in this way!' Having said this, Partha covered all sides with arrows inspired by mantras. And he also displayed his skill in shooting at an invisible mark by sound alone. And, O bull of the Bharata race, sorely afflicted with thirst, he discharged barbed darts and javelins and iron arrows, and showered on the sky innumerable shafts incapable of being baffled. Thereupon, the invisible Yaksha said, 'What need of all this trouble, O son of Pritha? Do thou drink only after answering my questions! If thou drink, however, without answering my questions, thou shalt die immediately after.' Thus addressed, Pritha's son Dhananjaya capable of drawing the bow with his left hand as well, disregarding those words, drank of the water, and immediately after dropped down dead. And (seeing Dhananjaya's delay) Kunti's son Yudhishthira addressed Bhimasena, saying, 'O represser of foes, it is a long while that Nakula and Sahadeva and Vibhatsu have gone to fetch water, and they have not come yet, O Bharata! Good betide thee! Do thou bring them back, together with water!' Thereupon saying, 'So be it,' Bhimasena set out for that place where those tigers among men, his brothers, lay dead. And beholding them, Bhima afflicted though he was with thirst, was exceedingly distressed. And that mighty armed hero thought all that to have been the act of some Yaksha or Rakshasa. And Pritha's son Vrikodara thought, 'I shall surely have to fight today. Let me, therefore, first appease my thirst.' Then that bull of the Bharata race rushed forward with the intention of drinking. Thereupon the Yaksha said, 'O child, do not commit this rash act! This lake hath already been in my possession. Do thou first answer my questions, and then drink and take away as much water as thou requirest!'"
Thus addressed by that Yaksha of immeasurable energy, Bhima, without answering his questions, drank of the water. And as soon as he drank, he fell down dead on the spot.
When none of his brothers returned, Yudhishthira became worried and started searching them. He too found the same lake. Then seeing his brothers condition he started lamenting and when he wanted to enter in the lake for ablution, the same voice warned him and Yudhishthira has agreed to answer his questions.
And having concluded this for certain, he began to perform his ablutions in that lake. And while he descended into it, he heard these words from the sky, uttered by the Yaksha,--'I am a crane, living on tiny fish. It is by me that thy younger brothers have been brought under the sway of the lord of departed spirits. If, thou, O prince, answer not the questions put by me, even thou shalt number the fifth corpse. Do not, O child, act rashly! This lake hath already been in my possession. Having answered my questions first, do thou, O Kunti's son, drink and carry away (as much as thou requirest)!' Hearing these words, Yudhishthira said, 'Art thou the foremost of the Rudras, or of the Vasus, or of the Marutas? I ask, what god art thou? This could not have been done by a bird! Who is it that hath overthrown the four mighty mountains, viz., the Himavat, the Paripatra, the Vindhya, and the Malaya? Great is the feat done by thee, thou foremost of strong persons! Those whom neither gods, nor Gandharvas nor Asuras, nor Rakshasas could endure in mighty conflict, have been slain by thee! Therefore, exceedingly wonderful is the deed done by thee! I do not know what thy business may be, nor do I know thy purpose. Therefore, great is the curiosity and fear also that have taken possession of me? My mind is greatly agitated, and as my head also is aching, I ask thee, therefore, O worshipful one, who art thou that stayest here?' Hearing these words the Yaksha said, 'I am, good betide thee, a Yaksha, and not an amphibious bird. It is by me that all these brothers of thine, endued with mighty prowess, have been slain!'
Hearing these accursed words couched in harsh syllabus, 1 Yudhishthira, O king, approaching the Yaksha who had spoken then, stood there. And that bull among the Bharatas then beheld that Yaksha of unusual eyes and huge body tall like a palmyra-palm and looking like fire or the Sun, and irresistible and gigantic like a mountain, staying on a tree, and uttering a loud roar deep as that of the clouds. And the Yaksha said, 'These thy brothers, O king, repeatedly forbidden by me, would forcibly take away water. It is for this that they have been slain by me! He that wisheth to live, should not, O king, drink this water! O son of Pritha, act not rashly! This lake hath already been in my possession. Do thou, O son of Kunti, first answer my questions, and then take away as much as thou likest!' Yudhishthira said, 'I do not, O Yaksha, covet, what is already in thy possession! O bull among male beings, virtuous persons never approve that one should applaud his own self (without boasting, I shall, therefore, answer thy questions, according to my intelligence). Do thou ask me!'
Then Yaksha has asked numerous questions and Yudhishthira has answered all questions correctly. You may read all questions and answers here.
Then pleased by Yudhishthira answers, Yaksha has asked him that which one of your brothers should become alive? Then Yudhishthira has told that he want Nakula to be alive. Yaksha asked him why he want Nakula, then Yudhishthira replied that his father has two wives, Kunti and Madri and he want atleast one son of Madri to be alive.
The Yaksha said,--'Thou hast, O king truly answered who is a man, and what man possesseth every kind of wealth. Therefore, let one only amongst thy brothers, whom thou mayst wish, get up with life!' Yudhishthira answered,--'Let this one that is of darkish hue, whose eyes are red, who is tall like a large Sala tree, whose chest is broad and arms long, let this Nakula, O Yaksha, get up with life! The Yaksha rejoined,-'This Bhimasena is dear unto thee, and this Arjuna also is one upon whom all of you depend! Why, then, O king dost thou, wish a step-brother to get up with his life! How canst thou, forsaking Bhima whose strength is equal to that of ten thousand elephants, wish Nakula to live? People said that this Bhima was dear to thee. From what motive then dost thou wish a step-brother to revive? Forsaking Arjuna the might of whose arm is worshipped by all the sons of Pandu, why dost thou wish Nakula to revive?' Yudhishthira said,--'If virtue is sacrificed, he that sacrificeth it, is himself lost. So virtue also cherisheth the cherisher. Therefore taking care that virtue by being sacrificed may not sacrifice us, I never forsake virtue. Abstention from injury is the highest virtue, and is, I ween, even higher than the highest object of attainment. I endeavour to practise that virtue. Therefore, let Nakula, O Yaksha, revive! Let men know that the king is always virtuous! I will never depart from my duty. Let Nakula, therefore, revive! My father had two wives, Kunti and Madri. Let both of them have children. This is what I wish. As Kunti is to me, so also is Madri. There is no difference between them in my eye. I desire to act equally towards my mothers. Therefore, let Nakula live?'
Then again pleased with Yudhishthira, Yaksha has revived all Pandavas.
The Yaksha said,--'Since abstention from injury is regarded by thee as higher than both profit and pleasure, therefore, let all thy brothers live, O bull of Bharata race!"