The contract will sustain around 100 jobs until the middle of 2019 and comes at time when Prospect has expressed concerns about the lack of work coming into the yard as a result of Ministry of Defence decisions on timings and procurement policies.
The work will be undertaken in the in the dry dock where HMS Queen Elizabeth was built.
Prospect is the largest union representing staff employed by Babcock at Rosyth Dockyard. Negotiator Jane Rose said: "We've seen a steady reduction in staffing numbers at Rosyth over the past year as work on the Navy's other carrier, HMS Prince of Wales nears completion.
"It's really good news that HMS Queen Elizabeth will return home for this maintenance work, but we do need to see more commitment from the MoD to British yards to ensure that skills and sovereign capability are retained."
Prospect's senior officer in Scotland, Richard Hardy added: "We've been working on this with local parliamentarians and through contacts with the Scottish Government, but the bigger issue is ensuring that the Rosyth dry dock has work to keep it from closing before Queen Elizabeth's next planned refit.
"We need the UK government to review its decision on the contract for the Fleet Solid Support ships (FSS) which will support and replenish the carriers at sea. We need a proper commitment from the UK government to its own National Shipbuilding Strategy"