Cristiano Ronaldo‘s team got the win it needed in the showdown against the Swiss at a packed Stadium of Light.
Both teams finished with 27 points, but Portugal had a better goal difference.
Switzerland, which had led Group B since the first round, will have to go through a playoff to try to make it to the tournament in Russia.
The Swiss arrived with a three-point lead over Euro 2016 winner Portugal and needed at least a draw to earn the automatic World Cup berth.
An own-goal by Swiss defender Johan Djourou before halftime and a goal by forward by Andre Silva early in the second half were enough to give Portugal its ninth straight victory since an opening 2-0 loss to the Swiss in Basel last year.
Switzerland had won all of its nine qualifiers until Tuesday’s setback.
Portugal was in control but hadn’t been able to create many scoring chances until Djourou found his own net while trying to clear a cross from the left by Eliseu in the 41st minute.
The ball touched Djourou’s right foot after getting past a defender and then Switzerland goalkeeper Yann Sommer.
Silva added to the lead in the 57th with a shot into the open net after picking up a cross from the right by Bernardo Silva.
It was only the second time Ronaldo failed to score in this qualifying campaign. The Real Madrid star ended with 15 goals, one less than Poland striker Robert Lewandowski, who scored a record-setting 16th on Sunday.
The result maintained Portugal’s seven-game winning streak at home.
Among those in the crowd at the Stadium of Light was Madonna.
Watch the Highlights: