In a tie that had been under the nation's spotlight since the start of the election campaign, Khairy 46, managed 48,250 votes but it was not good enough to challenge Pakatan Harapan's Datuk R. Ramanan, who secured 50,943 to win the 'hot seat' with a 2,693-vote majority.
It is the Health Minister's first defeat in a general election since 2008.
Perikatan Nasional's Mohd Ghazali Md Hamin came in third with 29,060 votes followed by Pejuang's Mohd Akmal Mohd Yusoff (829) and Ahmad Jufliz Faiza of Parti Rakyat Malaysia (PRM) with 279 votes.
Independent candidates with Syed Abdul Razak Syed Long Alsagoff and Nurhaslinda Basri only managed 165 and 113 votes respectively.
Election Commission Returning Officer Mohd Jusni Hashim announced the result at the Petaling District and Land Office Complex's Kenanga Hall here tonight.
With the victory, PH, represented by PKR, has now won the seat for four consecutive terms.
Speaking to reporters later, Ramanan thanked the Sungai Buloh electorate, PH chairman Datuk Seri Anwar Ibrahim and the party machinery for his win.
Khairy contested in Sungai Buloh this time around after UMNO deputy president Datuk Seri Mohamad Hasan was fielded in Rembau, where Khairy had been the incumbent MP for three terms.
In GE14, R. Sivarasa of PKR won in Sungai Buloh with a 26,634-vote majority to retain the seat he had won since 2008.
-- BERNAMA