Team INEOS rider Thomas sustained a pelvis fracture when riding over a drinks bottle prior to the start of stage three and announced on Tuesday he was unable to continue.
That decision was taken shortly before an eventful leg in Villafranca Tirrena, with Groupama–FDJ's Demare sneaking in ahead of Sagan and Ballerini in a photo finish.
Bora-Hansgrohe's Sagan launched his sprint with a little under 200m to go, with Demare to his left, but he finished a matter of millimetres behind his rival.
Italian riders Ballerini, Andrea Vendrame and Elia Viviani completed the top five.
"I think it was a millimetre," Demare said in his post-race interview. "I had luck on my side. I often train for sprints behind my dad's scooter. It helped me for today.
"I wasn't sure if I had won when I crossed the line."
Joao Almeida finished the stage safely in the front group and extended his overall lead over closest challenger Jonathan Caicedo to two seconds, having taken bonus seconds on the second intermediate sprint.
The tense finish was somewhat overshadowed by a nasty crash involving Vini-Zabu riders Luca Wackermann and Etienne van Empel, who appeared to be taken out by barriers blown into their path by a low-flying race helicopter.
On the unusually flat stage 11 along the Adriatic coast, Demare (Groupama-FDJ) produced a consistent performance that allowed for him to enjoy a late burst to add to his wins on stages four, six and seven.
Sagan (Bora-Hansgrohe) had looked to be on course for a second successive stage win after his first ever in the Giro on Tuesday, but his decision making in the final stages ultimately cost him.
While he initially opted to focus on following Fernando Gaviria (UAE Team Emirates) with less than 300 metres to go, Demare squeezed ahead and crossed the line a full length clear of Sagan.
Demare keeps hold of the Maglia Ciclamino, while Joao Almedia remains in control in the general classification battle with a 34-second lead, as the top three stayed out of trouble on a 182-kilometre route that was set up for the sprinters.
Frenchman Demare paid tribute to his team-mates, saying: "Impressive, really impressive. They had to bring the breakaway back and set a really high pace.
"It feels amazing. Of course, I didn't think I could get four stages here. I was looking for a stage win and I'm so happy.
"I really want to congratulate the whole team because it's their victory as well. They believed in me, everyone has worked really hard and the work is paying off."
Following Tuesday's spate of coronavirus-enforced withdrawals, there was an air of uncertainty heading into stage 11, but it was business as usual once the action started.
UAE Team Emirates and Cofidis looked dangerous in the mid-section but the latter were hamstrung shortly after the breakaway split with around 36km to go – Elia Viviani sent tumbling as a motorbike clipped him from behind.
In the latter stages Deceuninck-Quick-Step, UAE Team Emirates and INEOS Grenadiers riders came to the fore as Sander Armee (Lotto Soudal) began to flag at the front.
Demare and company arrived at the scene late on, and although they briefly lost the lead with under 2km to go, Miles Scotson and Jacopo Guarnieri expertly launched their team-mate to seal a dramatic win.
STAGE RESULT
1. Arnaud Demare (Groupama-FDJ) 04:03:52
2. Peter Sagan (Bora-Hansgrohe) +00:00
3. Alvaro Hodeg (Deceuninck-Quick-Step) +00:00
4. Simone Consonni (Cofidis) +00:00
5. Rick Zabel (Israel Start-Up Nation) +00:00
CLASSIFICATION STANDING
General Classification
1. Joao Almeida (Deceuninck-Quick-Step) 43:41:57
2. Wilco Kelderman (Team Sunweb) +00:34
3. Pello Bilbao (Bahrain-McLaren) +00:43
Points Classification
1. Arnaud Demare (Groupama-FDJ) 220
2. Peter Sagan (Bora-Hansgrohe) 184
3. Filippo Ganna (INEOS Grenadiers) 51
King of the Mountains
1. Ruben Guerreiro (EF Pro Cycling) 84
2. Giovanni Visconti (Vini Zabu-Brado-KTM) 76
3. Jonathan Castroviejo (INEOS Grenadiers), Filippo Ganna (INEOS Grenadiers) 45
WHAT'S NEXT?
Stage 12 sees the riders start and finish in the port town of Cesenatico, with the 204km route seeing them head into the mountainous hinterlands.
The Bora-Hansgrohe rider earned the first Giro stage victory of his decorated career with a daring effort on the 177-kilometre route from Lanciano to Tortoreto.
Deceuninck-Quick-Step's Joao Almeida extended his lead at the top of the general classification to 34 seconds on a day that started with a spate of coronavirus-enforced withdrawals.
It was not a happy outing for Jakob Fuglsang as he lost over a minute to Almeida, the Astana rider suffering a mechanical to fall well off the pace.
Despite Sagan's triumph, which means he has now taken stage honours at each of the Grand Tours, Arnaud Demare still has hold of the Maglia Ciclamino.
"I'm very happy, finally," said Sagan. "I've been trying since when we started the season again with Strade Bianche, San Remo, the Tour de France – I was a lot of time on the podium or top five but my last victory was last year in the Tour de France, already a long time ago.
"For sure I did not have the plan [to go solo]. When I already accepted second, third, fourth place, and I let it go and I don't try anymore – and now the victory comes. I'm very happy for that.
"Finally, I won in my style, I do the race, do some show, take the victory – it's something special."
Sagan was aggressive from the outset, leading a breakaway that was at one point five minutes ahead of the peloton.
Rain and a devilish course made for drama aplenty, but Almeida kept himself out of trouble to maintain a firm grip on his Maglia Rosa amid a chasing pack that was disrupted by sporadic breaks.
INEOS Grenadiers rider Ben Swift had been the only man to keep pace with Sagan heading into the last 20km, but the Slovakian proved too strong for the Briton.
The three-time world champion negotiated some horrendously steep climbs and a nerve-wracking descent in the wet to cross the line 19 seconds ahead of nearest rival Brandon McNulty.
STAGE RESULT
1. Peter Sagan (Bora-Hansgrohe) 04:01:56
2. Brandon McNulty (UAE Team Emirates) +00:19
3. Joao Almeida (Deceuninck-Quick-Step) +00:23
4. Ben Swift (INEOS Grenadiers) +00:23
5. Jai Hindley (Team Sunweb) +00:23
CLASSIFICATION STANDING
General Classification
1. Joao Almeida (Deceuninck-Quick-Step) 39:38:05
2. Wilco Kelderman (Team Sunweb) +00:34
3. Pello Bilbao (Bahrain-McLaren) +00:43
Points Classification
1. Arnaud Demare (Groupama-FDJ) 167
2. Peter Sagan (Bora-Hansgrohe) 147
3. Filippo Ganna (INEOS Grenadiers) 51
King of the Mountains
1. Ruben Guerreiro (EF Pro Cycling) 84
2. Giovanni Visconti (Vini Zabu-Brado-KTM) 76
3. Jonathan Castroviejo (INEOS Grenadiers), Filippo Ganna (INEOS Grenadiers) 45
WHAT'S NEXT?
Stage 11 sees the riders continue up the Adriatic coast on a relatively flat 182km route from Porto Sant'Elpidio to Rimini.
Seven-time Tour de France green jersey winner Sagan was forced to abandon the Tour de Suisse on Saturday due to a third positive coronavirus test in 18 months.
The Slovakian, who holds the record for the most points classification victories in the Tour de France (seven), took to Twitter to announce he had contracted the virus on Sunday.
"Yesterday, Saturday, after the finish of stage seven of the Tour de Suisse, I was given a COVID-19 test by the Team TotalEnergies doctor," he wrote.
"Unfortunately, it came out positive. I have no symptoms, and I feel well but I have to abandon the race. I thank you for your support and I will keep you posted."
Sagan becomes the latest name in a growing list of riders to contract the virus, with INEOS Grenadiers' Tom Pidcock and Bora-Hansgrohe's Aleksandr Vlasov also testing positive for COVID-19.
Vlasov was leading the Tour de Suisse at the time of his withdrawal, while Mikkel Bjerg – team-mate of two-time Tour de France winner Tadej Pogacar – withdrew from the Tour of Slovenia due to coronavirus.
Sagan will hope to recover in time for the start of the Tour de France in Copenhagen on July 1, having ended a 13-month winless drought with his sprint victory at stage three in Grenchen earlier this week.
Ulissi broke free from the peloton with around one kilometre to go on the final climb of the 149km route from Alcamo to Agrigento on Sunday.
Sagan stuck with Ulissi until the final sprint but was unable to prevent the Italian claiming a seventh career stage win at the Giro.
"It was a very hard climb, I did my maximum but Ulissi was stronger," Sagan said afterwards. "It was pretty hard, three kilometres at full gas."
There was another strong performance from Team INEOS' Geraint Thomas, who maintained his lead over fellow general classification contender Simon Yates.
Thomas is third in the overall standings, 23 seconds behind his INEOS team-mate Filippo Ganna, who has the lead following victory in Saturday's time trial.
However, Ganna's full focus is now on supporting team leader Thomas.
"The climb was really hard," Ganna said. "I've conserved the [Maglia Rosa] jersey, I'm really happy. From tomorrow, I'll work for the team because it's a really hard day and it can decide the jersey.
"[My role] is to support G [Thomas], obviously. I'm here for that and happy to support him."