A group of marine dinoflagellates (Alveolata, Eukaryota), consisting of ∼10 species of the genus Alexandrium, Gymnodinium catenatum and Pyrodinium bahamense, produce the toxin saxitoxin and its analogues (STX), which can accumulate in shellfish, leading to ecosystem and human health impacts. The genes, sxt, putatively involved in STX biosynthesis, have recently been identified, however, the evolution of these genes within dinoflagellates is not clear. There are two reasons for this: uncertainty over the phylogeny of dinoflagellates; and that the sxt genes of many species of Alexandrium and other dinoflagellate genera are not known. Here, we determined the phylogeny of STX-producing and other dinoflagellates based on a concatenated eight-gene alignment. We determined the presence, diversity and phylogeny of sxtA, domains A1 and A4 and sxtG in 52 strains of Alexandrium, and a further 43 species of dinoflagellates and thirteen other alveolates. We confirmed the presence and high sequence conservation of sxtA, domain A4, in 40 strains (35 Alexandrium, 1 Pyrodinium, 4 Gymnodinium) of 8 species of STX-producing dinoflagellates, and absence from non-producing species. We found three paralogs of sxtA, domain A1, and a widespread distribution of sxtA1 in non-STX producing dinoflagellates, indicating duplication events in the evolution of this gene. One paralog, clade 2, of sxtA1 may be particularly related to STX biosynthesis. Similarly, sxtG appears to be generally restricted to STX-producing species, while three amidinotransferase gene paralogs were found in dinoflagellates. We investigated the role of positive (diversifying) selection following duplication in sxtA1 and sxtG, and found negative selection in clades of sxtG and sxtA1, clade 2, suggesting they were functionally constrained. Significant episodic diversifying selection was found in some strains in clade 3 of sxtA1, a clade that may not be involved in STX biosynthesis, indicating pressure for diversification of function.