You can use
\resizebox{\fpeval{(2.5cm) / (5cm)}\textwidth}{!}{\figureB}
where \fpeval is provided by xfp.
Essentially you're resizing the image to a scaled version of \textwidth based on the ratio of the two figure's widths. This is known in this case, so it seems superfluous to evaluate (2.5cm) / (5cm) rather than just using .5. But in general this might not be known.
The following example is slightly different, using 2.5pc for the smaller image:

\documentclass{article}
\usepackage[paperwidth=12cm]{geometry}
\usepackage{graphicx,xfp}
\setlength{\parindent}{0pt}% Just for this example
\begin{document}
\def\figureA{\includegraphics[width=5cm]{example-image-a}}
\def\figureB{\includegraphics[width=2.5pc]{example-image-b}}
This is \texttt{figureA} with its natural size:
\figureA
\bigskip
This is \texttt{figureB} with its natural size:
\figureB
\bigskip
This is \texttt{figureA} scaled to fit the page width:
\resizebox{\textwidth}{!}{\figureA}
\bigskip
How do I rescale \texttt{figureB} to keep its relative size compared to \texttt{figureA} without knowing its actual size?
\bigskip
\resizebox{\fpeval{(2.5pc) / (5cm)}\textwidth}{!}{\figureB}
\end{document}
You could also use
\resizebox{\fpeval{\textwidth * (2.5pc) / (5cm)}pt}{!}{\figureB}
since the result from \fpeval on dimensions is expressed in points.
If the figure dimensions are unknown, you can store the figures in boxes from which one can readily extract the width and/or height. Here is the above example written with that in mind:
\documentclass{article}
\usepackage[paperwidth=12cm]{geometry}
\usepackage{graphicx,xfp}
\setlength{\parindent}{0pt}% Just for this example
\begin{document}
\newsavebox{\figureAbox}% Store figure a in a box
\savebox{\figureAbox}{\includegraphics[width=5cm]{example-image-a}}
\newsavebox{\figureBbox}% Store figure b in a box
\savebox{\figureBbox}{\includegraphics[width=2.5pc]{example-image-b}}
This is \texttt{figureA} with its natural size:
\usebox\figureAbox% figure a
\bigskip
This is \texttt{figureB} with its natural size:
\usebox\figureBbox% figure b
\bigskip
This is \texttt{figureA} scaled to fit the page width:
\resizebox{\textwidth}{!}{\usebox\figureAbox}% Scaled figure a
\bigskip
How do I rescale \texttt{figureB} to keep its relative size compared to \texttt{figureA} without knowing its actual size?
\bigskip
\resizebox{\fpeval{\textwidth * (\wd\figureBbox) / (\wd\figureAbox)}pt}{!}{\usebox\figureBbox}% Scaled figure b
\end{document}
:)(four if we count that @egreg already changed it on the first time). – Phelype Oleinik Jan 10 '18 at 11:07:)– Phelype Oleinik Jan 10 '18 at 11:12