NEW DELHI — Rahul Gandhi, the scion of India’s famous political dynasty, on Saturday took over as president of the main opposition Congress party, which faces a stiff challenge from Prime Minister Narendra Modi’s ruling Hindu nationalists.

Mr. Gandhi, who took the mantle from his mother, Sonia, at a party function, assumes leadership of a party that has been losing power to Mr. Modi’s Bharatiya Janata Party since 2014. Mr. Gandhi’s party has suffered humiliating defeats in recent state elections despite his active campaigning.

In a speech, Sonia Gandhi described her son as the new hope for the Congress party, as workers danced, burst firecrackers and distributed sweets to celebrate the generational shift in leadership.

Mr. Gandhi, 47, will be taking on Mr. Modi when the prime minister seeks a second, five-year term in 2019. Mr. Modi has vowed to create a Congress-free India while working for the rise of Hindu nationalist forces.