The film starts with a narrative by Farhan Qureshi (R. Madhavan) about himself, Raju Rastogi (Sharman Joshi), and Rancchoddas "Rancho" Shyamaldas Chanchad (Aamir Khan) are students at the Imperial College of Engineering, one of the best colleges in India. Farhan wants to become a wildlife photographer, but is pursuing an engineering degree to fulfil his father's wish, whereas Raju needs to improve his family's financial situation, and Rancho studies for the pleasure of building machines. Rancho incurs the wrath of college's dean, Professor Viru Sahastrabuddhe a.k.a. 'Virus' (Boman Irani), by giving creative and unorthodox answers, and later blames the rote-learning mentality of the institution for the suicide of fellow student Joy Lobo. Virus's model student is Chatur Ramalingam called "Silencer", (Omi Vaidya), whom Rancho once humiliates by replacing praise with an obscenity in the speech memorized by Chatur for delivery before the authorities. Meanwhile, Rancho falls in love with Virus's medical student daughter Pia (Kareena Kapoor).
The tensions escalate when the three friends break into Virus's house at night so Rancho can propose his own marriage to Pia, and then urinate on a door inside the compound. The next day, Virus threatens to expel Raju, who, unable to choose between betraying his friend and disappointing his family, attempts his own death. After extensive care by Pia and his friends, he awakes from a coma. Afterward, Farhan pursues photography, while Raju gives eccentric answers to an examination. Virus vows to make the course's final exam as hard as possible so that Raju is unable to graduate. Pia, in reply, tells Rancho of the exam, and he and Farhan steal the examination's questions for Raju who refuses to cheat. Virus sentences them to expulsion; but they earn a reprieve when Viru's pregnant elder daughter Mona (Mona Singh) goes into labour at the same time. A heavy storm cuts electricity, and Pia instructs Rancho to deliver the baby in the college common room via Voice over IP, after Rancho restores power using car batteries and a power inverter invented by Rancho. After the baby is apparently stillborn, Rancho resuscitates it; wherefore Virus allows them to take and pass their final exams. Rancho comes first and is awarded Virus's prize pen, retained hitherto for an especially-capable student.
Their story is framed as intermittent flashbacks from the present day, ten years afterward, in which Raju and Farhan are in search of Rancho. They are joined by Chatur, now a wealthy businessman, looking to seal a deal with scientist Phunsukh Wangdu. At the house ascribed to Rancho, they discover another individual (Jaaved Jaffrey) under that name, whom they blackmail by seizing his father's ashes and threatening to flush them down a toilet. Thus threatened, the householder reveals that their friend was an orphan servant boy who loved learning, while he himself disliked it, wherefore the family sent the servant to study in his master's place, until graduation, whereafter the master pockets the qualifications and the benefits thereof, while the impersonator becomes a schoolteacher in Ladakh.
Raju and Farhan then take Pia to Ladakh, where they see inventions resembling those of their friend. When the latter's friends ask his real name, he identifies himself as Phunsukh Wangdu, Chatur's prospective business partner. Upon learning this, Chatur is horrified and begs Phunsukh to establish the business relationship, apparently to no effect. Pia and Rancho kiss to closing credits.