Media playback is not supported on this device
Gabriel Barbosa got the first with a scrappy finish before new Manchester City signing Gabriel Jesus volleyed in.
Luan then finished off a fine team move before Barbosa's deflected strike ensured Brazil went through as Group A winners.
However, Argentina and London 2012 champions Mexico failed join them in the last eight.
Argentina finished third in Group D after they were held to a 1-1 draw by Honduras, while Mexico were beaten 1-0 by South Korea.
Brazil will play Colombia in the last eight, while Denmark still go through despite the defeat after a 1-1 draw between South Africa and Iraq meant they finished second in the group.
Hosts Brazil had come in for criticism after a lacklustre start to the tournament in which they were held to goalless draws by South Africa and Iraq.
Neymar's form in particular had frustrated Brazilian fans, but the Barcelona forward looked more like himself against Denmark, with a beautiful chipped pass in the build-up to his side's third a notable highlight.
Germany finished the group stage in dominant fashion as they beat Fiji 10-0.
Freiburg forward Nils Petersen hit five goals, Schalke midfielder Max Meyler grabbed a hat-trick and Arsenal's Serge Gnabry scored twice as Germany finished second in Group C to set up a last-eight match with Portugal.
The Portuguese finished top of Group D after drawing 1-1 with Algeria in Belo Horizonte.
Colombia progressed after beating Group B winners Nigeria 2-0. That result ended Japan's hopes of going through as they finished third in the group, despite beating Sweden 1-0.
Quarter-finals: Brazil v Colombia; Portugal v Germany; South Korea v Honduras; Nigeria v Denmark
Brazil reached the quarter-finals of Olympics men's football with an impressive 4-0 win against Denmark.