There was only one time in Pokémon Sun and Moon in which you could obtain a Marshadow: the Marshadow Distribution Event, in which you could by a card at various game stores depending on the country, and it would have a scratchable code that you could use to claim the Marshadow as a Mystery Gift. You also would have received its Z-Crystal. This is one of the several occurrences in SM/USUM of a mythical Pokémon only being obtainable through an event.
-- An answer by me
There is only one SM Magearna event as well. The item, moves, level, obtainment method, ect, are the same in all countries, which is why I'm calling it the same event, but the dates were different. In...
North America, the Event started on the 5th of December 2016
Japan, the event started on the 16th of July 2016 and ended the 30th of September 2016.
Europe, the event started on the 6th of December 2016
Notes:
- The event is still active in North America and Europe (Serebii said there was "No End Date")
- The Magearna is obtained via QR code
- This event is only available in SM and not USUM
Source for Magearna Event: Serebii Eventdex
If you are wondering about non-SM events that have occurred, there has been one for each. Magearna's, an in-game event, was in Pokemon HOME from the 14 of February 2020 to the present in America, Japan, and Eupope.
Marshadow's, a serial code event, was in SwSh from...
the 30th of October 2022 to the 12th November 2022 in America
the 9th of September 2022 to the 30th September 2022 in Europe
the 26th of April 2021 to the 31st of October 2021 in South Korea
the 20 November 2020 to the present in Japan
The Pokemon was identical throughout all of these times for both Pokemon.. See this page (Marshadow) and this page (Magearna) for sources and additional details.