The Greeks scored on the break when Kostas Tsimikas’s cross found Youssef El Arabi at the far post and the Moroccan beat keeper Manuel Neuer.
Bayern, struggling for form since their 7-2 win at Tottenham Hotspur earlier this month, recovered to level with Lewandowski in the 34th minute.
He then put them ahead, stabbing in from close range against the run of play in the 62nd for his 18th goal in 12 games in all competitions.
Corentin Tolisso added a third with a curled shot in the 75th before the Greeks cut the deficit with Guilherme’s deflected effort four minutes later.
The win lifted Bayern to nine point from three games. Tottenham crushed Red Star Belgrade 5-0 to move up to second place on four, one ahead of the Serbs. Olympiakos are bottom with one point.
“We made it difficult for ourselves again and didn’t have enough conviction,” said Neuer. “We need to continue working on that, analysing and seeing what we can do better.
“The problem at the moment is that we need to score three goals every game to win because we keep conceding two.”
While victory takes them a step closer to a spot in the knockout stages, it was far from a vintage performance from Bayern, who were again leaking goals.
They have now let in two goals in each of their last five games and there could be even more defensive trouble ahead after Niklas Suele tore his cruciate ligament in his left knee on Saturday and has been ruled out for months.
Fullback Lucas Hernandez, who was deployed into a central defender role in the absence of Suele, had to be taken off with what looked like a painful foot injury in the 58th minute.
But even before that Bayern had looked vulnerable at the back with El Arabi taking full advantage of Suele’s absence in the central defence to head in for the lead.
Coach Niko Kovac looked at times frustrated by the way his team was performing and it took Lewandowski’s scoring prowess to get them out of trouble once more.
Bayern, who have not won their last two league matches, thought they had wrapped it up when Tolisso netted their third but were again caught napping as the Greeks set up a dramatic finale.
However, they could not avoid their first home loss in Europe after a 10-game unbeaten run at their stadium.