Amygdalin is a cyanogenic glycoside widely used by many plants in herbivore defense. Poisonous to most animals, amygdalin-derived cyanide is detoxified by potent enzymes commonly found in bacteria and plants but not most animals. Here we show that the nematode C. elegans can detoxify amygdalin by a genetic pathway comprising cysl-1, egl-9, hif-1 and cysl-2. Essential for amygdalin resistance, cysl-1 encodes a protein similar to cysteine synthetic enzymes in bacteria and plants, but functionally co-opted in C. elegans. We identify exclusively HIF-activating egl-9 mutations in a cysl-1 suppressor screen and show that cysl-1 confers amygdalin resistance by regulating HIF-1-dependent cysl-2 transcription to protect against amygdalin toxicity. Phylogenetic analysis suggests cysl-1 and cysl-2 were acquired from green algae by horizontal gene transfer (HGT) and functionally co-opted by C. elegans in protection against amygdalin. Unlike functional HGTs from bacteria to eukaryotes that have been widely observed and characterized, our studies reveal previously unknown HGT from algae to nematodes, supporting the emerging theme that HGT-mediated evolutionary changes can facilitate host survival and adaptation to adverse environment stresses and biogenic toxins.