Short version: yes, but only if the pilots agree in advance that the safety pilot is acting PIC.
When to log PIC
Per 14 CFR 61.51 there are only three ways - ignoring training scenarios - to log PIC (my emphasis):
(i) When the pilot is the sole manipulator of the controls of an
aircraft for which the pilot is rated, or has sport pilot privileges
for that category and class of aircraft, if the aircraft class rating
is appropriate;
(ii) When the pilot is the sole occupant in the aircraft;
(iii) When the pilot, except for a holder of a sport or recreational
pilot certificate, acts as pilot in command of an aircraft for which
more than one pilot is required under the type certification of the
aircraft or the regulations under which the flight is conducted; or
In your scenario, both pilots are qualified to operate the aircraft under IFR, and two pilots are required for simulated IMC by regulation. Therefore both the first and third reasons are applicable, the question is how to 'match' them to the pilots and the key question there is: who is acting PIC?
Option 1: the safety pilot is acting PIC
If the safety pilot is acting PIC then he can log PIC time under point 3 because the regulations require two pilots for simulated IMC. And, since the 'pilot flying' (to borrow an airline term) is the one physically flying the aircraft, he can also log PIC as the sole manipulator of the controls.
Option 2: the pilot flying is acting PIC
If the 'pilot flying' is acting PIC then he can log PIC time under point 3 as described above. However the safety pilot can't: he isn't acting PIC, and he isn't manipulating the controls. In this case, the safety pilot can only log SIC time per 61.51(f)(2):
(f) Logging second-in-command flight time. A person may log
second-in-command time only for that flight time during which that
person:
[...]
(2) Holds the appropriate category, class, and instrument rating (if
an instrument rating is required for the flight) for the aircraft
being flown, and more than one pilot is required under the type
certification of the aircraft or the regulations under which the
flight is being conducted.
Of course, if the safety pilot ever takes the controls, then he can log PIC for that time under the first reason.
PIC responsibility
This might all seem like playing with words, and two friends going for a flight might not even discuss explicitly who's acting PIC, although they should. 14 CFR 91.3 says:
The pilot in command of an aircraft is directly responsible for, and
is the final authority as to, the operation of that aircraft.
What that means for this scenario is that if something goes wrong during the flight or a regulation is broken, the FAA will ask who was the acting PIC. If the pilots agreed that the safety pilot was PIC then the flight is his responsibility and he can't say that "I was just the lookout". Possibly even worse, if the pilots don't know who was acting PIC, then the FAA will give both of them a hard time.