Alam is the son of Mohammad Taslimuddin, the late MP from Araria, whose death necessitated the bye-election.
This was the first electoral battle in the state since Chief Minister Nitish Kumar, who also heads the JD (U), walked out of the alliance with the RJD and the Congress and joined the BJP-led NDA past year. "The bypoll results are victory of truth over lies", said Lalu Prasad Yadav, now lodged in a Ranchi jail, in a tweet.
The constituency has 35 per cent Muslim.
RJD leaders and workers celebrated across the state by distributing candies and bursting fire crackers.
The bypolls in Bihar, the first major elections in the state after Chief Minister Nitish Kumar broke away from the mahagathbandhan and tied up with the BJP, is expected to gauge the public response to Mr Kumar's decision to switch allies.
West Bengal Chief Minister Mamata Banerjee also congratulated Lalu for his party's win in the by-polls. Both are considered traditional RJD supporters. He also projected himself as a victim of a well-planned conspiracy hatched by the BJP and JD (U) by holding him accused in the land-for-hotel scam.
The BJP lost Gorakhpur seat to the SP by a margin of 21,961 votes.
BJP's Rinki Rani Pandey, wife of Anand Bhusan Pandey whose death caused the bypoll, defeated Congress rival Shambhu Singh Patel by about 14,000 votes in Bhabhua.
Deputy chief minister Sushil Kumar Modi said: "No political conclusions should be drawn from these bypolls as voters expressed a natural sympathy for the kin of the deceased leaders who were elected earlier". That idea will crush your arrogance, tweeted Mr Yadav.