The Deceuninck-Quick Step rider won the dash for the line after he was led out by teammate Julian Alaphilippe, the overall race leader and first Frenchman to don the leader's yellow jersey in five years.
Viviani edged Alexander Kristoff and Caleb Ewan who were second and third on a 1.5km home straight with all the top sprinters and their lead-off trains, were active over a nervy finale.
"At last! At last I have done what I set out to do," the 30-year-old Viviani said after his first ever Tour de France stage win.
"It's all for one and one for all in this team, you need to defend each other and friendship goes a long way in this sport," he said of the help Alaphilippe had given in winning his first Tour de France stage.
The unexpected role came after Kasper Asgreen suffered a bike-snapping fall on stage 3 and was relieved of lead-out duties Tuesday.
"I needed this win," said Viviani, who failed to take a stage at the Giro d'Italia in May.
Slovak Peter Sagan retained the lead in the points standings even though he faded to fourth in a photo finish with rookie Australian Ewan.
"I took his (Kristoff's) wheel and thought it was going to be close, but as you saw, it wasn't," said a relaxed looking Sagan.
"I've got the green jersey but I haven't got a stage win yet, I'll play it day by day - it's a long way to Paris," said the Slovak star after pulling on the green jersey again.
- Thomas wary of falling -
Defending champion Geraint Thomas had a quiet day as he, co-captain Egan Bernal and their Ineos teammates all finished in the pack.
The bunch sprint was not the only high speed section of the stage as there ws a long, steep descent into Nancy
"On the descent with 10km to go I didn't dare look at my Garmin (speedometer). I was just looking in front, making sure I stayed upright," said Thomas, who had a scare on stage 1 when he flew over barriers but avoided injury.
Thomas remains 45 sec adrift of Alaphilippe in the overall standings.
Alaphilippe was the toast of the Tour on Tuesday as fans all along the route shouted 'Juju', his nickname, as he rode by in the yellow jersey he won the day before with a swashbuckling 15km attack in stage three.
"From the moment I got off the bus, all the way to the finish line it was just overwhelming," said the 27-year-old.
Another rider creating quite a buzz at the Tour is Belgian debutant Wout van Aert, who headed the Jumbo Visma team with another show of cool power on a stage raced across open plains.
A nervy Richie Porte was seen shouting at a TV motorbike that had strayed too close on Tuesday. In the same situation, van Aert offered the cameraman his water bidon on the sunny afternoon.
A trio adventured out on a long-range breakaway early in the stage. with Michael Schar being awarded the combativity award after he, Frederik Backaert and Yoann Offredo held on until the Nancy city limits.
Roof-top stork's nests and postcard Alsace villages are on the menu Wednesday, when the Tour traverses eastern France's Vosges mountains with two third category and two second category climbs from Saint-Die-des-Vosges to Colmar.
The first true mountain stage follows on Thursday with a finish atop La Planche des Belles Filles, where Chris Froome, out of this year's Ineos team with injury, won in 2012, his then Sky captain Bradley Wiggins going on to win the Tour.
Since then two Italians, Vincenzo Nibali in 2014 and Fabio Aru in 2017 have taken emotional wins on the Vosges peak.
Results from stage 4 of the Tour de France on Tuesday a 213.5km run from Reims to Nancy:
1. Elia Viviani (ITA/DEC) 5hrs 09mins 2sec
2. Alexander Kristoff (NOR/UAE) same time
3. Caleb Ewan (AUS/LOT) at 0.
4. Peter Sagan (SVK/BOR) 0.
5. Dylan Groenewegen (NED/JUM) 0.
6. Mike Teunissen (NED/JUM) 0.
7. Giacomo Nizzolo (ITA/DDT) 0.
8. Jasper Stuyven (BEL/TRE) 0.
9. Michael Matthews (AUS/SUN) 0.
10. Christophe Laporte (FRA/COF) 0.
11. Matteo Trentin (ITA/MIT) 0.
12. Andre Greipel (GER/ARK) 0.
13. Niccolo Bonifazio (ITA/TDE) 0.
14. Matej Mohoric (SLO/BAH) 0.
15. Ivan Garcia (ESP/BAH) 0.
16. Andrea Pasqualon (ITA/WGG) 0.
17. Rick Zabel (GER/KAT) 0.
18. Sonny Colbrelli (ITA/BAH) 0.
19. Maximiliano Richeze (ARG/DEC) 0.
20. Guillaume Martin (FRA/WGG) 0.
Selected: (all same time)
23. Egan Bernal (COL/INE) 0.
24. Vincenzo Nibali (ITA/BAH) 0.
26. Nairo Quintana (COL/MOV) 0.
37. Roman Kreuziger (CZE/DDT) 0.
42. Steven Kruijswijk (NED/JUM) 0.
53. Thibaut Pinot (FRA/FDJ) 0.
54. Geraint Thomas (GBR/INE) 0.
56. Julian Alaphilippe (FRA/DEC) 0.
57. Mikel Landa (ESP/MOV) 0.
64. Michael Woods (CAN/EF1) 0.
69. Richie Porte (AUS/TRE) 0.
92. Fabio Aru (ITA/UAE) 0.
135. Luke Rowe (GBR/INE) 1:17.
150. Simon Yates (GBR/MIT) 1:45.