As it turns out, the phrase has been around since the mid-19th century, and it was coined by historian Thomas Carlyle. At the time, the skills required for writing poetry were referred to as the "gay science," so Carlyle decided to call economics the "dismal science" as a clever turn of phrase.
The popular belief is that Carlyle started using the phrase in response to the "dismal" prediction of 19th-century reverend and scholar Thomas Malthus, who forecasted that the rate of growth in the food supply as compared to the rate of the growth in population would result in mass starvation. (Luckily for us, Malthus' assumptions regarding technological progress were overly, well, dismal, and such mass starvation never transpired.)
While Carlyle did use the word dismal in reference to Malthus' findings, he didn't use the phrase "dismal science" until his 1849 work Occasional Discourse on the Negro Question. In this piece, Carlyle argued that reintroducing (or continuing) slavery would be morally superior to relying on the market forces of supply and demand, and he labeled the profession of economists who disagreed with him, most notably John Stuart Mill, as the "dismal science," since Carlyle believed that the emancipation of slaves would leave them worse off. (This prediction has also turned out to be incorrect, of course.)