Tangency of the contour lines to the constraint curve is not a necessary condition.
To the Wiki article Lagrange multiplier, there is a note "Inaccurate intuition" criticizing the article for promoting the false intuition that the extrama of the function occurs when the level curves are tangent to the constraint curve. The author of the note gives an example whose simplified version is as follows.
Take $$f(x,y)=x^2$$ whose 3D plot and level curves look like as follows
The constraint curve is a circle centered at the origin (blue line): $$g(x,y)=x^2+y^2-1=0$$
the minima occur at $(0,-1)$ and $(0,1)$ at which points the contor lines are perpendicular to the constraint. The two maxima occur at points where the constraint curve is, indeed, tangent to the contours: $(-1,0)$ and $(1,0)$.
This, however, does not mean that the Lagrange multiplier method does not work. Take the Lagrange function $$\mathscr L(x,y,\lambda)=x^2+\lambda(x^2+y^2-1)$$
and take the partial derivatives and set them equal to zero:
For $\lambda=0$: $x=0$ and $y=\pm 1$ and for $\lambda=-1$: $y=0$ and $x=\pm1$ as the intuition already has shown.