The poem was the last Cowper composed, the manuscript being dated, according to James Sambrook in his 1994 edition of the Longman Annotated Texts, 20th March 1799 at the beginning and 25th March at the end.
The poem, the full text of which is available by clicking the link above, was inspired by the description in Richard Walter's A Voyage around the World by George Anson (1748) of how, on 24th March 1741 (48 years to the day before Cowper's writing), Anson's Centurion had lost overboard an able seaman during a gale south of Tierra del Fuego. Unable to turn their vessel, they had watched as the seaman bravely swam through the waves, notwithstanding the “horror attending his …
Clark, Robert. "The Castaway". The Literary Encyclopedia. First published 18 January 2007
[http://www.litencyc.com/php/sworks.php?rec=true&UID=1353, accessed 25 March 2017.]